blob: e90bbd2a2ea678f2c743403fa57adf80332b1762 [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 +000034static void wait_stm50flw0x0x(chipaddr bios)
Claus Gindharta7b35512008-04-28 17:51:09 +000035{
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000036 chip_writeb(0x70, bios);
37 if ((chip_readb(bios) & 0x80) == 0) { // it's busy
38 while ((chip_readb(bios) & 0x80) == 0) ;
Claus Gindharta7b35512008-04-28 17:51:09 +000039 }
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000040
Claus Gindharta7b35512008-04-28 17:51:09 +000041 // put another command to get out of status register mode
42
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000043 chip_writeb(0x90, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000044 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +000045
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000046 chip_readb(bios); // Read device ID (to make sure?)
Claus Gindharta7b35512008-04-28 17:51:09 +000047
48 // this is needed to jam it out of "read id" mode
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000049 chip_writeb(0xAA, bios + 0x5555);
50 chip_writeb(0x55, bios + 0x2AAA);
51 chip_writeb(0xF0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000052}
53
54/*
55 * claus.gindhart@kontron.com
56 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +000057 * before you can erase them or write to them.
58 */
Claus Gindharta7b35512008-04-28 17:51:09 +000059int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
60{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000061 chipaddr wrprotect = flash->virtual_registers + 2;
Claus Gindharta7b35512008-04-28 17:51:09 +000062 const uint8_t unlock_sector = 0x00;
63 int j;
64
Uwe Hermann394131e2008-10-18 21:14:13 +000065 /*
66 * These chips have to be unlocked before you can erase them or write
67 * to them. The size of the locking sectors depends on the type
68 * of chip.
69 *
70 * Sometimes, the BIOS does this for you; so you propably
71 * don't need to worry about that.
72 */
Claus Gindharta7b35512008-04-28 17:51:09 +000073
Uwe Hermann394131e2008-10-18 21:14:13 +000074 /* Check, if it's is a top/bottom-block with 4k-sectors. */
75 /* TODO: What about the other types? */
Claus Gindharta7b35512008-04-28 17:51:09 +000076 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +000077 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
78 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +000079
80 // unlock each 4k-sector
81 for (j = 0; j < 0x10000; j += 0x1000) {
82 printf_debug("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000083 chip_writeb(unlock_sector, wrprotect + offset + j);
84 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Uwe Hermann394131e2008-10-18 21:14:13 +000085 printf("Cannot unlock sector @ 0x%x\n",
86 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +000087 return -1;
88 }
89 }
90 } else {
91 printf_debug("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000092 chip_writeb(unlock_sector, wrprotect + offset);
93 if (chip_readb(wrprotect + offset) != unlock_sector) {
Claus Gindharta7b35512008-04-28 17:51:09 +000094 printf("Cannot unlock sector @ 0x%x\n", offset);
95 return -1;
96 }
97 }
98
99 return 0;
100}
101
102int erase_block_stm50flw0x0x(struct flashchip *flash, int offset)
103{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000104 chipaddr bios = flash->virtual_memory + offset;
Claus Gindharta7b35512008-04-28 17:51:09 +0000105
106 // clear status register
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000107 chip_writeb(0x50, bios);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000108 printf_debug("Erase at 0x%lx\n", bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000109 // now start it
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000110 chip_writeb(0x20, bios);
111 chip_writeb(0xd0, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000112 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +0000113
114 wait_stm50flw0x0x(flash->virtual_memory);
115
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000116 if (check_erased_range(flash, offset, flash->page_size)) {
117 fprintf(stderr, "ERASE FAILED!\n");
118 return -1;
Claus Gindharta7b35512008-04-28 17:51:09 +0000119 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000120 printf("DONE BLOCK 0x%x\n", offset);
121
122 return 0;
123}
124
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000125int write_page_stm50flw0x0x(chipaddr bios, uint8_t *src,
126 chipaddr dst, int page_size)
Claus Gindharta7b35512008-04-28 17:51:09 +0000127{
Uwe Hermann394131e2008-10-18 21:14:13 +0000128 int i, rc = 0;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000129 chipaddr d = dst;
Claus Gindharta7b35512008-04-28 17:51:09 +0000130 uint8_t *s = src;
131
132 /* transfer data from source to destination */
133 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000134 chip_writeb(0x40, dst);
135 chip_writeb(*src++, dst++);
Claus Gindharta7b35512008-04-28 17:51:09 +0000136 wait_stm50flw0x0x(bios);
137 }
138
139/* claus.gindhart@kontron.com
140 * TODO
141 * I think, that verification is not required, but
142 * i leave it in anyway
143 */
144 dst = d;
145 src = s;
146 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000147 if (chip_readb(dst) != *src) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000148 rc = -1;
149 break;
150 }
151 dst++;
152 src++;
153 }
154
155 if (rc) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000156 fprintf(stderr, " page 0x%lx failed!\n",
157 (d - bios) / page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000158 }
159
160 return rc;
161}
162
163/* I simply erase block by block
164 * I Chip This is not the fastest way, but it works
165 */
166int erase_stm50flw0x0x(struct flashchip *flash)
167{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000168 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +0000169 int total_size = flash->total_size * 1024;
170 int page_size = flash->page_size;
Claus Gindharta7b35512008-04-28 17:51:09 +0000171
172 printf("Erasing page:\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000173 for (i = 0; i < total_size / page_size; i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000174 printf
175 ("\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");
176 printf("%04d at address: 0x%08x ", i, i * page_size);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000177 if (unlock_block_stm50flw0x0x(flash, i * page_size)) {
178 fprintf(stderr, "UNLOCK FAILED!\n");
179 return -1;
180 }
181 if (erase_block_stm50flw0x0x(flash, i * page_size)) {
182 fprintf(stderr, "ERASE FAILED!\n");
183 return -1;
184 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000185 }
186 printf("\n");
Claus Gindharta7b35512008-04-28 17:51:09 +0000187
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000188 return 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000189}
190
191int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf)
192{
Uwe Hermann394131e2008-10-18 21:14:13 +0000193 int i, rc = 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000194 int total_size = flash->total_size * 1024;
195 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000196 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000197 uint8_t *tmpbuf = malloc(page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000198
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000199 if (!tmpbuf) {
200 printf("Could not allocate memory!\n");
201 exit(1);
202 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000203 printf("Programming page: \n");
Uwe Hermann394131e2008-10-18 21:14:13 +0000204 for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000205 printf
206 ("\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");
207 printf("%04d at address: 0x%08x ", i, i * page_size);
208
209 /* Auto Skip Blocks, which already contain the desired data
210 * Faster, because we only write, what has changed
211 * More secure, because blocks, which are excluded
212 * (with the exclude or layout feature)
213 * are not erased and rewritten; data is retained also
214 * in sudden power off situations
215 */
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000216 chip_readn(tmpbuf, bios + i * page_size, page_size);
217 if (!memcmp((void *)(buf + i * page_size), tmpbuf, page_size)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000218 printf("SKIPPED\n");
219 continue;
220 }
221
222 rc = unlock_block_stm50flw0x0x(flash, i * page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000223 if (!rc)
224 rc = erase_block_stm50flw0x0x(flash, i * page_size);
225 if (!rc)
226 write_page_stm50flw0x0x(bios, buf + i * page_size,
227 bios + i * page_size, page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000228 }
229 printf("\n");
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000230 free(tmpbuf);
Claus Gindharta7b35512008-04-28 17:51:09 +0000231
232 return rc;
233}