blob: b2469f3fff858de71050abe4b9df6a8ad1d89b13 [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>
Claus Gindharta7b35512008-04-28 17:51:09 +000030#include "flash.h"
31
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000032void protect_stm50flw0x0x(chipaddr bios)
Claus Gindharta7b35512008-04-28 17:51:09 +000033{
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000034 chip_writeb(0xAA, bios + 0x5555);
35 chip_writeb(0x55, bios + 0x2AAA);
36 chip_writeb(0xA0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000037
38 usleep(200);
39}
40
41int probe_stm50flw0x0x(struct flashchip *flash)
42{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000043 chipaddr bios = flash->virtual_memory;
Claus Gindharta7b35512008-04-28 17:51:09 +000044 uint8_t id1, id2;
45 uint32_t largeid1, largeid2;
46
47 /* Issue JEDEC Product ID Entry command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000048 chip_writeb(0xAA, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000049 myusec_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000050 chip_writeb(0x55, bios + 0x2AAA);
Claus Gindharta7b35512008-04-28 17:51:09 +000051 myusec_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000052 chip_writeb(0x90, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000053 myusec_delay(40);
54
55 /* Read product ID */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000056 id1 = chip_readb(bios);
57 id2 = chip_readb(bios + 0x01);
Claus Gindharta7b35512008-04-28 17:51:09 +000058 largeid1 = id1;
59 largeid2 = id2;
60
61 /* Check if it is a continuation ID, this should be a while loop. */
62 if (id1 == 0x7F) {
63 largeid1 <<= 8;
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000064 id1 = chip_readb(bios + 0x100);
Claus Gindharta7b35512008-04-28 17:51:09 +000065 largeid1 |= id1;
66 }
67 if (id2 == 0x7F) {
68 largeid2 <<= 8;
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000069 id2 = chip_readb(bios + 0x101);
Claus Gindharta7b35512008-04-28 17:51:09 +000070 largeid2 |= id2;
71 }
72
73 /* Issue JEDEC Product ID Exit command */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000074 chip_writeb(0xAA, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000075 myusec_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000076 chip_writeb(0x55, bios + 0x2AAA);
Claus Gindharta7b35512008-04-28 17:51:09 +000077 myusec_delay(10);
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000078 chip_writeb(0xF0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000079 myusec_delay(40);
80
Peter Stuge5cafc332009-01-25 23:52:45 +000081 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, largeid1,
Claus Gindharta7b35512008-04-28 17:51:09 +000082 largeid2);
83
84 if (largeid1 != flash->manufacture_id || largeid2 != flash->model_id)
85 return 0;
86
87 map_flash_registers(flash);
88
89 return 1;
90}
91
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000092static void wait_stm50flw0x0x(chipaddr bios)
Claus Gindharta7b35512008-04-28 17:51:09 +000093{
94 uint8_t id1;
95 // id2;
96
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000097 chip_writeb(0x70, bios);
98 if ((chip_readb(bios) & 0x80) == 0) { // it's busy
99 while ((chip_readb(bios) & 0x80) == 0) ;
Claus Gindharta7b35512008-04-28 17:51:09 +0000100 }
101 // put another command to get out of status register mode
102
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000103 chip_writeb(0x90, bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000104 myusec_delay(10);
105
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000106 id1 = chip_readb(bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000107
108 // this is needed to jam it out of "read id" mode
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000109 chip_writeb(0xAA, bios + 0x5555);
110 chip_writeb(0x55, bios + 0x2AAA);
111 chip_writeb(0xF0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +0000112}
113
114/*
115 * claus.gindhart@kontron.com
116 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +0000117 * before you can erase them or write to them.
118 */
Claus Gindharta7b35512008-04-28 17:51:09 +0000119int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
120{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000121 chipaddr wrprotect = flash->virtual_registers + 2;
Claus Gindharta7b35512008-04-28 17:51:09 +0000122 const uint8_t unlock_sector = 0x00;
123 int j;
124
Uwe Hermann394131e2008-10-18 21:14:13 +0000125 /*
126 * These chips have to be unlocked before you can erase them or write
127 * to them. The size of the locking sectors depends on the type
128 * of chip.
129 *
130 * Sometimes, the BIOS does this for you; so you propably
131 * don't need to worry about that.
132 */
Claus Gindharta7b35512008-04-28 17:51:09 +0000133
Uwe Hermann394131e2008-10-18 21:14:13 +0000134 /* Check, if it's is a top/bottom-block with 4k-sectors. */
135 /* TODO: What about the other types? */
Claus Gindharta7b35512008-04-28 17:51:09 +0000136 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +0000137 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
138 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000139
140 // unlock each 4k-sector
141 for (j = 0; j < 0x10000; j += 0x1000) {
142 printf_debug("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +0000143 chip_writeb(unlock_sector, wrprotect + offset + j);
144 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000145 printf("Cannot unlock sector @ 0x%x\n",
146 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +0000147 return -1;
148 }
149 }
150 } else {
151 printf_debug("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +0000152 chip_writeb(unlock_sector, wrprotect + offset);
153 if (chip_readb(wrprotect + offset) != unlock_sector) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000154 printf("Cannot unlock sector @ 0x%x\n", offset);
155 return -1;
156 }
157 }
158
159 return 0;
160}
161
162int erase_block_stm50flw0x0x(struct flashchip *flash, int offset)
163{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000164 chipaddr bios = flash->virtual_memory + offset;
Claus Gindharta7b35512008-04-28 17:51:09 +0000165 int j;
166
167 // clear status register
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000168 chip_writeb(0x50, bios);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000169 printf_debug("Erase at 0x%lx\n", bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000170 // now start it
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000171 chip_writeb(0x20, bios);
172 chip_writeb(0xd0, bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000173 myusec_delay(10);
174
175 wait_stm50flw0x0x(flash->virtual_memory);
176
177 for (j = 0; j < flash->page_size; j++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000178 if (chip_readb(bios + j) != 0xFF) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000179 printf("Erase failed at 0x%x\n", offset + j);
180 return -1;
181 }
182 }
183
184 printf("DONE BLOCK 0x%x\n", offset);
185
186 return 0;
187}
188
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000189int write_page_stm50flw0x0x(chipaddr bios, uint8_t *src,
190 chipaddr dst, int page_size)
Claus Gindharta7b35512008-04-28 17:51:09 +0000191{
Uwe Hermann394131e2008-10-18 21:14:13 +0000192 int i, rc = 0;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000193 chipaddr d = dst;
Claus Gindharta7b35512008-04-28 17:51:09 +0000194 uint8_t *s = src;
195
196 /* transfer data from source to destination */
197 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000198 chip_writeb(0x40, dst);
199 chip_writeb(*src++, dst++);
Claus Gindharta7b35512008-04-28 17:51:09 +0000200 wait_stm50flw0x0x(bios);
201 }
202
203/* claus.gindhart@kontron.com
204 * TODO
205 * I think, that verification is not required, but
206 * i leave it in anyway
207 */
208 dst = d;
209 src = s;
210 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000211 if (chip_readb(dst) != *src) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000212 rc = -1;
213 break;
214 }
215 dst++;
216 src++;
217 }
218
219 if (rc) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000220 fprintf(stderr, " page 0x%lx failed!\n",
221 (d - bios) / page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000222 }
223
224 return rc;
225}
226
227/* I simply erase block by block
228 * I Chip This is not the fastest way, but it works
229 */
230int erase_stm50flw0x0x(struct flashchip *flash)
231{
Uwe Hermann394131e2008-10-18 21:14:13 +0000232 int i, rc = 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000233 int total_size = flash->total_size * 1024;
234 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000235 chipaddr bios = flash->virtual_memory;
Claus Gindharta7b35512008-04-28 17:51:09 +0000236
237 printf("Erasing page:\n");
Uwe Hermann394131e2008-10-18 21:14:13 +0000238 for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000239 printf
240 ("\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");
241 printf("%04d at address: 0x%08x ", i, i * page_size);
242 rc = unlock_block_stm50flw0x0x(flash, i * page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000243 if (!rc)
244 rc = erase_block_stm50flw0x0x(flash, i * page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000245 }
246 printf("\n");
247 protect_stm50flw0x0x(bios);
248
249 return rc;
250}
251
252int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf)
253{
Uwe Hermann394131e2008-10-18 21:14:13 +0000254 int i, rc = 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000255 int total_size = flash->total_size * 1024;
256 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000257 chipaddr bios = flash->virtual_memory;
Claus Gindharta7b35512008-04-28 17:51:09 +0000258
259 printf("Programming page: \n");
Uwe Hermann394131e2008-10-18 21:14:13 +0000260 for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000261 printf
262 ("\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");
263 printf("%04d at address: 0x%08x ", i, i * page_size);
264
265 /* Auto Skip Blocks, which already contain the desired data
266 * Faster, because we only write, what has changed
267 * More secure, because blocks, which are excluded
268 * (with the exclude or layout feature)
269 * are not erased and rewritten; data is retained also
270 * in sudden power off situations
271 */
272 if (!memcmp((void *)(buf + i * page_size),
273 (void *)(bios + i * page_size), page_size)) {
274 printf("SKIPPED\n");
275 continue;
276 }
277
278 rc = unlock_block_stm50flw0x0x(flash, i * page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000279 if (!rc)
280 rc = erase_block_stm50flw0x0x(flash, i * page_size);
281 if (!rc)
282 write_page_stm50flw0x0x(bios, buf + i * page_size,
283 bios + i * page_size, page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000284 }
285 printf("\n");
286 protect_stm50flw0x0x(bios);
287
288 return rc;
289}