blob: 0ae6eb814d4ef7518a6bdf8571b39bb177267be4 [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
Claus Gindharta7b35512008-04-28 17:51:09 +000034int probe_stm50flw0x0x(struct flashchip *flash)
35{
Carl-Daniel Hailfinger4e9cebb2009-09-05 01:16:30 +000036 int result = probe_jedec(flash);
Claus Gindharta7b35512008-04-28 17:51:09 +000037
Carl-Daniel Hailfinger4e9cebb2009-09-05 01:16:30 +000038 if (!result)
39 return result;
Claus Gindharta7b35512008-04-28 17:51:09 +000040
41 map_flash_registers(flash);
42
43 return 1;
44}
45
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000046static void wait_stm50flw0x0x(chipaddr bios)
Claus Gindharta7b35512008-04-28 17:51:09 +000047{
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000048 chip_writeb(0x70, bios);
49 if ((chip_readb(bios) & 0x80) == 0) { // it's busy
50 while ((chip_readb(bios) & 0x80) == 0) ;
Claus Gindharta7b35512008-04-28 17:51:09 +000051 }
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000052
Claus Gindharta7b35512008-04-28 17:51:09 +000053 // put another command to get out of status register mode
54
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000055 chip_writeb(0x90, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000056 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +000057
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000058 chip_readb(bios); // Read device ID (to make sure?)
Claus Gindharta7b35512008-04-28 17:51:09 +000059
60 // this is needed to jam it out of "read id" mode
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000061 chip_writeb(0xAA, bios + 0x5555);
62 chip_writeb(0x55, bios + 0x2AAA);
63 chip_writeb(0xF0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000064}
65
66/*
67 * claus.gindhart@kontron.com
68 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +000069 * before you can erase them or write to them.
70 */
Claus Gindharta7b35512008-04-28 17:51:09 +000071int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
72{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000073 chipaddr wrprotect = flash->virtual_registers + 2;
Claus Gindharta7b35512008-04-28 17:51:09 +000074 const uint8_t unlock_sector = 0x00;
75 int j;
76
Uwe Hermann394131e2008-10-18 21:14:13 +000077 /*
78 * These chips have to be unlocked before you can erase them or write
79 * to them. The size of the locking sectors depends on the type
80 * of chip.
81 *
82 * Sometimes, the BIOS does this for you; so you propably
83 * don't need to worry about that.
84 */
Claus Gindharta7b35512008-04-28 17:51:09 +000085
Uwe Hermann394131e2008-10-18 21:14:13 +000086 /* Check, if it's is a top/bottom-block with 4k-sectors. */
87 /* TODO: What about the other types? */
Claus Gindharta7b35512008-04-28 17:51:09 +000088 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +000089 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
90 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +000091
92 // unlock each 4k-sector
93 for (j = 0; j < 0x10000; j += 0x1000) {
94 printf_debug("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000095 chip_writeb(unlock_sector, wrprotect + offset + j);
96 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Uwe Hermann394131e2008-10-18 21:14:13 +000097 printf("Cannot unlock sector @ 0x%x\n",
98 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +000099 return -1;
100 }
101 }
102 } else {
103 printf_debug("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +0000104 chip_writeb(unlock_sector, wrprotect + offset);
105 if (chip_readb(wrprotect + offset) != unlock_sector) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000106 printf("Cannot unlock sector @ 0x%x\n", offset);
107 return -1;
108 }
109 }
110
111 return 0;
112}
113
114int erase_block_stm50flw0x0x(struct flashchip *flash, int offset)
115{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000116 chipaddr bios = flash->virtual_memory + offset;
Claus Gindharta7b35512008-04-28 17:51:09 +0000117
118 // clear status register
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000119 chip_writeb(0x50, bios);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000120 printf_debug("Erase at 0x%lx\n", bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000121 // now start it
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000122 chip_writeb(0x20, bios);
123 chip_writeb(0xd0, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000124 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +0000125
126 wait_stm50flw0x0x(flash->virtual_memory);
127
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000128 if (check_erased_range(flash, offset, flash->page_size)) {
129 fprintf(stderr, "ERASE FAILED!\n");
130 return -1;
Claus Gindharta7b35512008-04-28 17:51:09 +0000131 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000132 printf("DONE BLOCK 0x%x\n", offset);
133
134 return 0;
135}
136
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000137int write_page_stm50flw0x0x(chipaddr bios, uint8_t *src,
138 chipaddr dst, int page_size)
Claus Gindharta7b35512008-04-28 17:51:09 +0000139{
Uwe Hermann394131e2008-10-18 21:14:13 +0000140 int i, rc = 0;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000141 chipaddr d = dst;
Claus Gindharta7b35512008-04-28 17:51:09 +0000142 uint8_t *s = src;
143
144 /* transfer data from source to destination */
145 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000146 chip_writeb(0x40, dst);
147 chip_writeb(*src++, dst++);
Claus Gindharta7b35512008-04-28 17:51:09 +0000148 wait_stm50flw0x0x(bios);
149 }
150
151/* claus.gindhart@kontron.com
152 * TODO
153 * I think, that verification is not required, but
154 * i leave it in anyway
155 */
156 dst = d;
157 src = s;
158 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000159 if (chip_readb(dst) != *src) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000160 rc = -1;
161 break;
162 }
163 dst++;
164 src++;
165 }
166
167 if (rc) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000168 fprintf(stderr, " page 0x%lx failed!\n",
169 (d - bios) / page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000170 }
171
172 return rc;
173}
174
175/* I simply erase block by block
176 * I Chip This is not the fastest way, but it works
177 */
178int erase_stm50flw0x0x(struct flashchip *flash)
179{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000180 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +0000181 int total_size = flash->total_size * 1024;
182 int page_size = flash->page_size;
Claus Gindharta7b35512008-04-28 17:51:09 +0000183
184 printf("Erasing page:\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000185 for (i = 0; i < total_size / page_size; i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000186 printf
187 ("\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");
188 printf("%04d at address: 0x%08x ", i, i * page_size);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000189 if (unlock_block_stm50flw0x0x(flash, i * page_size)) {
190 fprintf(stderr, "UNLOCK FAILED!\n");
191 return -1;
192 }
193 if (erase_block_stm50flw0x0x(flash, i * page_size)) {
194 fprintf(stderr, "ERASE FAILED!\n");
195 return -1;
196 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000197 }
198 printf("\n");
Claus Gindharta7b35512008-04-28 17:51:09 +0000199
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000200 return 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000201}
202
203int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf)
204{
Uwe Hermann394131e2008-10-18 21:14:13 +0000205 int i, rc = 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000206 int total_size = flash->total_size * 1024;
207 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000208 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000209 uint8_t *tmpbuf = malloc(page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000210
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000211 if (!tmpbuf) {
212 printf("Could not allocate memory!\n");
213 exit(1);
214 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000215 printf("Programming page: \n");
Uwe Hermann394131e2008-10-18 21:14:13 +0000216 for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000217 printf
218 ("\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");
219 printf("%04d at address: 0x%08x ", i, i * page_size);
220
221 /* Auto Skip Blocks, which already contain the desired data
222 * Faster, because we only write, what has changed
223 * More secure, because blocks, which are excluded
224 * (with the exclude or layout feature)
225 * are not erased and rewritten; data is retained also
226 * in sudden power off situations
227 */
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000228 chip_readn(tmpbuf, bios + i * page_size, page_size);
229 if (!memcmp((void *)(buf + i * page_size), tmpbuf, page_size)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000230 printf("SKIPPED\n");
231 continue;
232 }
233
234 rc = unlock_block_stm50flw0x0x(flash, i * page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000235 if (!rc)
236 rc = erase_block_stm50flw0x0x(flash, i * page_size);
237 if (!rc)
238 write_page_stm50flw0x0x(bios, buf + i * page_size,
239 bios + i * page_size, page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000240 }
241 printf("\n");
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000242 free(tmpbuf);
Claus Gindharta7b35512008-04-28 17:51:09 +0000243
244 return rc;
245}