blob: 3b564365ce487a8ab3aae5d501f88983806a4b9c [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>
Sean Nelson56358aa2010-01-19 16:08:51 +00005 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
Claus Gindharta7b35512008-04-28 17:51:09 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Claus Gindharta7b35512008-04-28 17:51:09 +000020 */
21
22/*
23 * This module is designed for supporting the devices
24 * ST M50FLW040A (not yet tested)
25 * ST M50FLW040B (not yet tested)
26 * ST M50FLW080A
27 * ST M50FLW080B (not yet tested)
Claus Gindharta7b35512008-04-28 17:51:09 +000028 */
29
Claus Gindharta7b35512008-04-28 17:51:09 +000030#include <string.h>
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +000031#include <stdlib.h>
Claus Gindharta7b35512008-04-28 17:51:09 +000032#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000033#include "flashchips.h"
Claus Gindharta7b35512008-04-28 17:51:09 +000034
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000035static void wait_stm50flw0x0x(chipaddr bios)
Claus Gindharta7b35512008-04-28 17:51:09 +000036{
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000037 chip_writeb(0x70, bios);
38 if ((chip_readb(bios) & 0x80) == 0) { // it's busy
39 while ((chip_readb(bios) & 0x80) == 0) ;
Claus Gindharta7b35512008-04-28 17:51:09 +000040 }
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000041
Claus Gindharta7b35512008-04-28 17:51:09 +000042 // put another command to get out of status register mode
43
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000044 chip_writeb(0x90, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000045 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +000046
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000047 chip_readb(bios); // Read device ID (to make sure?)
Claus Gindharta7b35512008-04-28 17:51:09 +000048
49 // this is needed to jam it out of "read id" mode
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000050 chip_writeb(0xAA, bios + 0x5555);
51 chip_writeb(0x55, bios + 0x2AAA);
52 chip_writeb(0xF0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000053}
54
55/*
56 * claus.gindhart@kontron.com
57 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +000058 * before you can erase them or write to them.
59 */
Claus Gindharta7b35512008-04-28 17:51:09 +000060int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
61{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000062 chipaddr wrprotect = flash->virtual_registers + 2;
Claus Gindharta7b35512008-04-28 17:51:09 +000063 const uint8_t unlock_sector = 0x00;
64 int j;
65
Uwe Hermann394131e2008-10-18 21:14:13 +000066 /*
67 * These chips have to be unlocked before you can erase them or write
68 * to them. The size of the locking sectors depends on the type
69 * of chip.
70 *
71 * Sometimes, the BIOS does this for you; so you propably
72 * don't need to worry about that.
73 */
Claus Gindharta7b35512008-04-28 17:51:09 +000074
Uwe Hermann394131e2008-10-18 21:14:13 +000075 /* Check, if it's is a top/bottom-block with 4k-sectors. */
76 /* TODO: What about the other types? */
Claus Gindharta7b35512008-04-28 17:51:09 +000077 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +000078 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
79 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +000080
81 // unlock each 4k-sector
82 for (j = 0; j < 0x10000; j += 0x1000) {
83 printf_debug("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000084 chip_writeb(unlock_sector, wrprotect + offset + j);
85 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Uwe Hermann394131e2008-10-18 21:14:13 +000086 printf("Cannot unlock sector @ 0x%x\n",
87 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +000088 return -1;
89 }
90 }
91 } else {
92 printf_debug("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000093 chip_writeb(unlock_sector, wrprotect + offset);
94 if (chip_readb(wrprotect + offset) != unlock_sector) {
Claus Gindharta7b35512008-04-28 17:51:09 +000095 printf("Cannot unlock sector @ 0x%x\n", offset);
96 return -1;
97 }
98 }
99
100 return 0;
101}
102
Sean Nelson56358aa2010-01-19 16:08:51 +0000103int erase_block_stm50flw0x0x(struct flashchip *flash, unsigned int block, unsigned int blocksize)
Claus Gindharta7b35512008-04-28 17:51:09 +0000104{
Sean Nelson56358aa2010-01-19 16:08:51 +0000105 chipaddr bios = flash->virtual_memory + block;
Claus Gindharta7b35512008-04-28 17:51:09 +0000106
107 // clear status register
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000108 chip_writeb(0x50, bios);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000109 printf_debug("Erase at 0x%lx\n", bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000110 // now start it
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000111 chip_writeb(0x20, bios);
112 chip_writeb(0xd0, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000113 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +0000114
115 wait_stm50flw0x0x(flash->virtual_memory);
116
Sean Nelson56358aa2010-01-19 16:08:51 +0000117 if (check_erased_range(flash, block, blocksize)) {
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000118 fprintf(stderr, "ERASE FAILED!\n");
119 return -1;
Claus Gindharta7b35512008-04-28 17:51:09 +0000120 }
Sean Nelson56358aa2010-01-19 16:08:51 +0000121 printf("DONE BLOCK 0x%x\n", block);
122
123 return 0;
124}
125
126int erase_sector_stm50flw0x0x(struct flashchip *flash, unsigned int sector, unsigned int sectorsize)
127{
128 chipaddr bios = flash->virtual_memory + sector;
129
130 // clear status register
131 chip_writeb(0x50, bios);
132 printf_debug("Erase at 0x%lx\n", bios);
133 // now start it
134 chip_writeb(0x32, bios);
135 chip_writeb(0xd0, bios);
136 programmer_delay(10);
137
138 wait_stm50flw0x0x(flash->virtual_memory);
139
140 if (check_erased_range(flash, sector, sectorsize)) {
141 fprintf(stderr, "ERASE FAILED!\n");
142 return -1;
143 }
144 printf("DONE BLOCK 0x%x\n", sector);
Claus Gindharta7b35512008-04-28 17:51:09 +0000145
146 return 0;
147}
148
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000149int write_page_stm50flw0x0x(chipaddr bios, uint8_t *src,
150 chipaddr dst, int page_size)
Claus Gindharta7b35512008-04-28 17:51:09 +0000151{
Uwe Hermann394131e2008-10-18 21:14:13 +0000152 int i, rc = 0;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000153 chipaddr d = dst;
Claus Gindharta7b35512008-04-28 17:51:09 +0000154 uint8_t *s = src;
155
156 /* transfer data from source to destination */
157 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000158 chip_writeb(0x40, dst);
159 chip_writeb(*src++, dst++);
Claus Gindharta7b35512008-04-28 17:51:09 +0000160 wait_stm50flw0x0x(bios);
161 }
162
163/* claus.gindhart@kontron.com
164 * TODO
165 * I think, that verification is not required, but
166 * i leave it in anyway
167 */
168 dst = d;
169 src = s;
170 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000171 if (chip_readb(dst) != *src) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000172 rc = -1;
173 break;
174 }
175 dst++;
176 src++;
177 }
178
179 if (rc) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000180 fprintf(stderr, " page 0x%lx failed!\n",
181 (d - bios) / page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000182 }
183
184 return rc;
185}
186
187/* I simply erase block by block
188 * I Chip This is not the fastest way, but it works
189 */
190int erase_stm50flw0x0x(struct flashchip *flash)
191{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000192 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +0000193 int total_size = flash->total_size * 1024;
194 int page_size = flash->page_size;
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 }
Sean Nelson56358aa2010-01-19 16:08:51 +0000205 if (erase_block_stm50flw0x0x(flash, i * page_size, page_size)) {
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000206 fprintf(stderr, "ERASE FAILED!\n");
207 return -1;
208 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000209 }
210 printf("\n");
Claus Gindharta7b35512008-04-28 17:51:09 +0000211
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000212 return 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000213}
214
Sean Nelson56358aa2010-01-19 16:08:51 +0000215int erase_chip_stm50flw0x0x(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
216{
217 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
218 msg_cerr("%s called with incorrect arguments\n",
219 __func__);
220 return -1;
221 }
222 return erase_stm50flw0x0x(flash);
223}
224
Claus Gindharta7b35512008-04-28 17:51:09 +0000225int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf)
226{
Uwe Hermann394131e2008-10-18 21:14:13 +0000227 int i, rc = 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000228 int total_size = flash->total_size * 1024;
229 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000230 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000231 uint8_t *tmpbuf = malloc(page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000232
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000233 if (!tmpbuf) {
234 printf("Could not allocate memory!\n");
235 exit(1);
236 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000237 printf("Programming 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
243 /* Auto Skip Blocks, which already contain the desired data
244 * Faster, because we only write, what has changed
245 * More secure, because blocks, which are excluded
246 * (with the exclude or layout feature)
247 * are not erased and rewritten; data is retained also
248 * in sudden power off situations
249 */
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000250 chip_readn(tmpbuf, bios + i * page_size, page_size);
251 if (!memcmp((void *)(buf + i * page_size), tmpbuf, page_size)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000252 printf("SKIPPED\n");
253 continue;
254 }
255
256 rc = unlock_block_stm50flw0x0x(flash, i * page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000257 if (!rc)
Sean Nelson56358aa2010-01-19 16:08:51 +0000258 rc = erase_block_stm50flw0x0x(flash, i * page_size, page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000259 if (!rc)
260 write_page_stm50flw0x0x(bios, buf + i * page_size,
261 bios + i * page_size, page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000262 }
263 printf("\n");
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000264 free(tmpbuf);
Claus Gindharta7b35512008-04-28 17:51:09 +0000265
266 return rc;
267}