blob: 8cbafe2b5d3727d941b12f2d0db196d77010bf04 [file] [log] [blame]
Claus Gindharta7b35512008-04-28 17:51:09 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Claus Gindharta7b35512008-04-28 17:51:09 +000019 */
20
21/*
22 * This module is designed for supporting the devices
23 * ST M50FLW040A (not yet tested)
24 * ST M50FLW040B (not yet tested)
25 * ST M50FLW080A
26 * ST M50FLW080B (not yet tested)
Claus Gindharta7b35512008-04-28 17:51:09 +000027 */
28
Claus Gindharta7b35512008-04-28 17:51:09 +000029#include <string.h>
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +000030#include <stdlib.h>
Claus Gindharta7b35512008-04-28 17:51:09 +000031#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000032#include "flashchips.h"
Claus Gindharta7b35512008-04-28 17:51:09 +000033
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000034void protect_stm50flw0x0x(chipaddr bios)
Claus Gindharta7b35512008-04-28 17:51:09 +000035{
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000036 chip_writeb(0xAA, bios + 0x5555);
37 chip_writeb(0x55, bios + 0x2AAA);
38 chip_writeb(0xA0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000039
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000040 programmer_delay(200);
Claus Gindharta7b35512008-04-28 17:51:09 +000041}
42
43int probe_stm50flw0x0x(struct flashchip *flash)
44{
Carl-Daniel Hailfinger4e9cebb2009-09-05 01:16:30 +000045 int result = probe_jedec(flash);
Claus Gindharta7b35512008-04-28 17:51:09 +000046
Carl-Daniel Hailfinger4e9cebb2009-09-05 01:16:30 +000047 if (!result)
48 return result;
Claus Gindharta7b35512008-04-28 17:51:09 +000049
50 map_flash_registers(flash);
51
52 return 1;
53}
54
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000055static void wait_stm50flw0x0x(chipaddr bios)
Claus Gindharta7b35512008-04-28 17:51:09 +000056{
57 uint8_t id1;
58 // id2;
59
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000060 chip_writeb(0x70, bios);
61 if ((chip_readb(bios) & 0x80) == 0) { // it's busy
62 while ((chip_readb(bios) & 0x80) == 0) ;
Claus Gindharta7b35512008-04-28 17:51:09 +000063 }
64 // put another command to get out of status register mode
65
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000066 chip_writeb(0x90, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000067 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +000068
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000069 id1 = chip_readb(bios);
Claus Gindharta7b35512008-04-28 17:51:09 +000070
71 // this is needed to jam it out of "read id" mode
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000072 chip_writeb(0xAA, bios + 0x5555);
73 chip_writeb(0x55, bios + 0x2AAA);
74 chip_writeb(0xF0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000075}
76
77/*
78 * claus.gindhart@kontron.com
79 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +000080 * before you can erase them or write to them.
81 */
Claus Gindharta7b35512008-04-28 17:51:09 +000082int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
83{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000084 chipaddr wrprotect = flash->virtual_registers + 2;
Claus Gindharta7b35512008-04-28 17:51:09 +000085 const uint8_t unlock_sector = 0x00;
86 int j;
87
Uwe Hermann394131e2008-10-18 21:14:13 +000088 /*
89 * These chips have to be unlocked before you can erase them or write
90 * to them. The size of the locking sectors depends on the type
91 * of chip.
92 *
93 * Sometimes, the BIOS does this for you; so you propably
94 * don't need to worry about that.
95 */
Claus Gindharta7b35512008-04-28 17:51:09 +000096
Uwe Hermann394131e2008-10-18 21:14:13 +000097 /* Check, if it's is a top/bottom-block with 4k-sectors. */
98 /* TODO: What about the other types? */
Claus Gindharta7b35512008-04-28 17:51:09 +000099 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +0000100 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
101 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000102
103 // unlock each 4k-sector
104 for (j = 0; j < 0x10000; j += 0x1000) {
105 printf_debug("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +0000106 chip_writeb(unlock_sector, wrprotect + offset + j);
107 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000108 printf("Cannot unlock sector @ 0x%x\n",
109 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +0000110 return -1;
111 }
112 }
113 } else {
114 printf_debug("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +0000115 chip_writeb(unlock_sector, wrprotect + offset);
116 if (chip_readb(wrprotect + offset) != unlock_sector) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000117 printf("Cannot unlock sector @ 0x%x\n", offset);
118 return -1;
119 }
120 }
121
122 return 0;
123}
124
125int erase_block_stm50flw0x0x(struct flashchip *flash, int offset)
126{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000127 chipaddr bios = flash->virtual_memory + offset;
Claus Gindharta7b35512008-04-28 17:51:09 +0000128
129 // clear status register
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000130 chip_writeb(0x50, bios);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000131 printf_debug("Erase at 0x%lx\n", bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000132 // now start it
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000133 chip_writeb(0x20, bios);
134 chip_writeb(0xd0, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000135 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +0000136
137 wait_stm50flw0x0x(flash->virtual_memory);
138
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000139 if (check_erased_range(flash, offset, flash->page_size)) {
140 fprintf(stderr, "ERASE FAILED!\n");
141 return -1;
Claus Gindharta7b35512008-04-28 17:51:09 +0000142 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000143 printf("DONE BLOCK 0x%x\n", offset);
144
145 return 0;
146}
147
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000148int write_page_stm50flw0x0x(chipaddr bios, uint8_t *src,
149 chipaddr dst, int page_size)
Claus Gindharta7b35512008-04-28 17:51:09 +0000150{
Uwe Hermann394131e2008-10-18 21:14:13 +0000151 int i, rc = 0;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000152 chipaddr d = dst;
Claus Gindharta7b35512008-04-28 17:51:09 +0000153 uint8_t *s = src;
154
155 /* transfer data from source to destination */
156 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000157 chip_writeb(0x40, dst);
158 chip_writeb(*src++, dst++);
Claus Gindharta7b35512008-04-28 17:51:09 +0000159 wait_stm50flw0x0x(bios);
160 }
161
162/* claus.gindhart@kontron.com
163 * TODO
164 * I think, that verification is not required, but
165 * i leave it in anyway
166 */
167 dst = d;
168 src = s;
169 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000170 if (chip_readb(dst) != *src) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000171 rc = -1;
172 break;
173 }
174 dst++;
175 src++;
176 }
177
178 if (rc) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000179 fprintf(stderr, " page 0x%lx failed!\n",
180 (d - bios) / page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000181 }
182
183 return rc;
184}
185
186/* I simply erase block by block
187 * I Chip This is not the fastest way, but it works
188 */
189int erase_stm50flw0x0x(struct flashchip *flash)
190{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000191 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +0000192 int total_size = flash->total_size * 1024;
193 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000194 chipaddr bios = flash->virtual_memory;
Claus Gindharta7b35512008-04-28 17:51:09 +0000195
196 printf("Erasing page:\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000197 for (i = 0; i < total_size / page_size; i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000198 printf
199 ("\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");
200 printf("%04d at address: 0x%08x ", i, i * page_size);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000201 if (unlock_block_stm50flw0x0x(flash, i * page_size)) {
202 fprintf(stderr, "UNLOCK FAILED!\n");
203 return -1;
204 }
205 if (erase_block_stm50flw0x0x(flash, i * page_size)) {
206 fprintf(stderr, "ERASE FAILED!\n");
207 return -1;
208 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000209 }
210 printf("\n");
211 protect_stm50flw0x0x(bios);
212
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000213 return 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000214}
215
216int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf)
217{
Uwe Hermann394131e2008-10-18 21:14:13 +0000218 int i, rc = 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000219 int total_size = flash->total_size * 1024;
220 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000221 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000222 uint8_t *tmpbuf = malloc(page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000223
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000224 if (!tmpbuf) {
225 printf("Could not allocate memory!\n");
226 exit(1);
227 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000228 printf("Programming page: \n");
Uwe Hermann394131e2008-10-18 21:14:13 +0000229 for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000230 printf
231 ("\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");
232 printf("%04d at address: 0x%08x ", i, i * page_size);
233
234 /* Auto Skip Blocks, which already contain the desired data
235 * Faster, because we only write, what has changed
236 * More secure, because blocks, which are excluded
237 * (with the exclude or layout feature)
238 * are not erased and rewritten; data is retained also
239 * in sudden power off situations
240 */
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000241 chip_readn(tmpbuf, bios + i * page_size, page_size);
242 if (!memcmp((void *)(buf + i * page_size), tmpbuf, page_size)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000243 printf("SKIPPED\n");
244 continue;
245 }
246
247 rc = unlock_block_stm50flw0x0x(flash, i * page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000248 if (!rc)
249 rc = erase_block_stm50flw0x0x(flash, i * page_size);
250 if (!rc)
251 write_page_stm50flw0x0x(bios, buf + i * page_size,
252 bios + i * page_size, page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000253 }
254 printf("\n");
255 protect_stm50flw0x0x(bios);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000256 free(tmpbuf);
Claus Gindharta7b35512008-04-28 17:51:09 +0000257
258 return rc;
259}