blob: 44819e5e5ccb465823c41e6e6ea15114d00508dd [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{
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000057 chip_writeb(0x70, bios);
58 if ((chip_readb(bios) & 0x80) == 0) { // it's busy
59 while ((chip_readb(bios) & 0x80) == 0) ;
Claus Gindharta7b35512008-04-28 17:51:09 +000060 }
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000061
Claus Gindharta7b35512008-04-28 17:51:09 +000062 // put another command to get out of status register mode
63
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000064 chip_writeb(0x90, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000065 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +000066
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000067 chip_readb(bios); // Read device ID (to make sure?)
Claus Gindharta7b35512008-04-28 17:51:09 +000068
69 // this is needed to jam it out of "read id" mode
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000070 chip_writeb(0xAA, bios + 0x5555);
71 chip_writeb(0x55, bios + 0x2AAA);
72 chip_writeb(0xF0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000073}
74
75/*
76 * claus.gindhart@kontron.com
77 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +000078 * before you can erase them or write to them.
79 */
Claus Gindharta7b35512008-04-28 17:51:09 +000080int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
81{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000082 chipaddr wrprotect = flash->virtual_registers + 2;
Claus Gindharta7b35512008-04-28 17:51:09 +000083 const uint8_t unlock_sector = 0x00;
84 int j;
85
Uwe Hermann394131e2008-10-18 21:14:13 +000086 /*
87 * These chips have to be unlocked before you can erase them or write
88 * to them. The size of the locking sectors depends on the type
89 * of chip.
90 *
91 * Sometimes, the BIOS does this for you; so you propably
92 * don't need to worry about that.
93 */
Claus Gindharta7b35512008-04-28 17:51:09 +000094
Uwe Hermann394131e2008-10-18 21:14:13 +000095 /* Check, if it's is a top/bottom-block with 4k-sectors. */
96 /* TODO: What about the other types? */
Claus Gindharta7b35512008-04-28 17:51:09 +000097 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +000098 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
99 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000100
101 // unlock each 4k-sector
102 for (j = 0; j < 0x10000; j += 0x1000) {
103 printf_debug("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +0000104 chip_writeb(unlock_sector, wrprotect + offset + j);
105 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000106 printf("Cannot unlock sector @ 0x%x\n",
107 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +0000108 return -1;
109 }
110 }
111 } else {
112 printf_debug("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +0000113 chip_writeb(unlock_sector, wrprotect + offset);
114 if (chip_readb(wrprotect + offset) != unlock_sector) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000115 printf("Cannot unlock sector @ 0x%x\n", offset);
116 return -1;
117 }
118 }
119
120 return 0;
121}
122
123int erase_block_stm50flw0x0x(struct flashchip *flash, int offset)
124{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000125 chipaddr bios = flash->virtual_memory + offset;
Claus Gindharta7b35512008-04-28 17:51:09 +0000126
127 // clear status register
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000128 chip_writeb(0x50, bios);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000129 printf_debug("Erase at 0x%lx\n", bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000130 // now start it
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000131 chip_writeb(0x20, bios);
132 chip_writeb(0xd0, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000133 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +0000134
135 wait_stm50flw0x0x(flash->virtual_memory);
136
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000137 if (check_erased_range(flash, offset, flash->page_size)) {
138 fprintf(stderr, "ERASE FAILED!\n");
139 return -1;
Claus Gindharta7b35512008-04-28 17:51:09 +0000140 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000141 printf("DONE BLOCK 0x%x\n", offset);
142
143 return 0;
144}
145
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000146int write_page_stm50flw0x0x(chipaddr bios, uint8_t *src,
147 chipaddr dst, int page_size)
Claus Gindharta7b35512008-04-28 17:51:09 +0000148{
Uwe Hermann394131e2008-10-18 21:14:13 +0000149 int i, rc = 0;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000150 chipaddr d = dst;
Claus Gindharta7b35512008-04-28 17:51:09 +0000151 uint8_t *s = src;
152
153 /* transfer data from source to destination */
154 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000155 chip_writeb(0x40, dst);
156 chip_writeb(*src++, dst++);
Claus Gindharta7b35512008-04-28 17:51:09 +0000157 wait_stm50flw0x0x(bios);
158 }
159
160/* claus.gindhart@kontron.com
161 * TODO
162 * I think, that verification is not required, but
163 * i leave it in anyway
164 */
165 dst = d;
166 src = s;
167 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000168 if (chip_readb(dst) != *src) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000169 rc = -1;
170 break;
171 }
172 dst++;
173 src++;
174 }
175
176 if (rc) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000177 fprintf(stderr, " page 0x%lx failed!\n",
178 (d - bios) / page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000179 }
180
181 return rc;
182}
183
184/* I simply erase block by block
185 * I Chip This is not the fastest way, but it works
186 */
187int erase_stm50flw0x0x(struct flashchip *flash)
188{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000189 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +0000190 int total_size = flash->total_size * 1024;
191 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000192 chipaddr bios = flash->virtual_memory;
Claus Gindharta7b35512008-04-28 17:51:09 +0000193
194 printf("Erasing page:\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000195 for (i = 0; i < total_size / page_size; i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000196 printf
197 ("\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");
198 printf("%04d at address: 0x%08x ", i, i * page_size);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000199 if (unlock_block_stm50flw0x0x(flash, i * page_size)) {
200 fprintf(stderr, "UNLOCK FAILED!\n");
201 return -1;
202 }
203 if (erase_block_stm50flw0x0x(flash, i * page_size)) {
204 fprintf(stderr, "ERASE FAILED!\n");
205 return -1;
206 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000207 }
208 printf("\n");
209 protect_stm50flw0x0x(bios);
210
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000211 return 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000212}
213
214int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf)
215{
Uwe Hermann394131e2008-10-18 21:14:13 +0000216 int i, rc = 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000217 int total_size = flash->total_size * 1024;
218 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000219 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000220 uint8_t *tmpbuf = malloc(page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000221
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000222 if (!tmpbuf) {
223 printf("Could not allocate memory!\n");
224 exit(1);
225 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000226 printf("Programming page: \n");
Uwe Hermann394131e2008-10-18 21:14:13 +0000227 for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000228 printf
229 ("\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");
230 printf("%04d at address: 0x%08x ", i, i * page_size);
231
232 /* Auto Skip Blocks, which already contain the desired data
233 * Faster, because we only write, what has changed
234 * More secure, because blocks, which are excluded
235 * (with the exclude or layout feature)
236 * are not erased and rewritten; data is retained also
237 * in sudden power off situations
238 */
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000239 chip_readn(tmpbuf, bios + i * page_size, page_size);
240 if (!memcmp((void *)(buf + i * page_size), tmpbuf, page_size)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000241 printf("SKIPPED\n");
242 continue;
243 }
244
245 rc = unlock_block_stm50flw0x0x(flash, i * page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000246 if (!rc)
247 rc = erase_block_stm50flw0x0x(flash, i * page_size);
248 if (!rc)
249 write_page_stm50flw0x0x(bios, buf + i * page_size,
250 bios + i * page_size, page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000251 }
252 printf("\n");
253 protect_stm50flw0x0x(bios);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000254 free(tmpbuf);
Claus Gindharta7b35512008-04-28 17:51:09 +0000255
256 return rc;
257}