blob: 7095aecef1bd15f1c5cd995a9fa55629e146a54d [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"
Sean Nelson14ba6682010-02-26 05:48:29 +000034#include "chipdrivers.h"
Claus Gindharta7b35512008-04-28 17:51:09 +000035
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000036static void wait_stm50flw0x0x(chipaddr bios)
Claus Gindharta7b35512008-04-28 17:51:09 +000037{
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000038 chip_writeb(0x70, bios);
39 if ((chip_readb(bios) & 0x80) == 0) { // it's busy
40 while ((chip_readb(bios) & 0x80) == 0) ;
Claus Gindharta7b35512008-04-28 17:51:09 +000041 }
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000042
Claus Gindharta7b35512008-04-28 17:51:09 +000043 // put another command to get out of status register mode
44
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000045 chip_writeb(0x90, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000046 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +000047
Stefan Reinauer9e72aa52009-09-16 08:18:08 +000048 chip_readb(bios); // Read device ID (to make sure?)
Claus Gindharta7b35512008-04-28 17:51:09 +000049
50 // this is needed to jam it out of "read id" mode
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000051 chip_writeb(0xAA, bios + 0x5555);
52 chip_writeb(0x55, bios + 0x2AAA);
53 chip_writeb(0xF0, bios + 0x5555);
Claus Gindharta7b35512008-04-28 17:51:09 +000054}
55
56/*
57 * claus.gindhart@kontron.com
58 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +000059 * before you can erase them or write to them.
60 */
Claus Gindharta7b35512008-04-28 17:51:09 +000061int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
62{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000063 chipaddr wrprotect = flash->virtual_registers + 2;
Claus Gindharta7b35512008-04-28 17:51:09 +000064 const uint8_t unlock_sector = 0x00;
65 int j;
66
Uwe Hermann394131e2008-10-18 21:14:13 +000067 /*
68 * These chips have to be unlocked before you can erase them or write
69 * to them. The size of the locking sectors depends on the type
70 * of chip.
71 *
72 * Sometimes, the BIOS does this for you; so you propably
73 * don't need to worry about that.
74 */
Claus Gindharta7b35512008-04-28 17:51:09 +000075
Uwe Hermann394131e2008-10-18 21:14:13 +000076 /* Check, if it's is a top/bottom-block with 4k-sectors. */
77 /* TODO: What about the other types? */
Claus Gindharta7b35512008-04-28 17:51:09 +000078 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +000079 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
80 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +000081
82 // unlock each 4k-sector
83 for (j = 0; j < 0x10000; j += 0x1000) {
84 printf_debug("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000085 chip_writeb(unlock_sector, wrprotect + offset + j);
86 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Uwe Hermann394131e2008-10-18 21:14:13 +000087 printf("Cannot unlock sector @ 0x%x\n",
88 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +000089 return -1;
90 }
91 }
92 } else {
93 printf_debug("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000094 chip_writeb(unlock_sector, wrprotect + offset);
95 if (chip_readb(wrprotect + offset) != unlock_sector) {
Claus Gindharta7b35512008-04-28 17:51:09 +000096 printf("Cannot unlock sector @ 0x%x\n", offset);
97 return -1;
98 }
99 }
100
101 return 0;
102}
103
Sean Nelson56358aa2010-01-19 16:08:51 +0000104int erase_block_stm50flw0x0x(struct flashchip *flash, unsigned int block, unsigned int blocksize)
Claus Gindharta7b35512008-04-28 17:51:09 +0000105{
Sean Nelson56358aa2010-01-19 16:08:51 +0000106 chipaddr bios = flash->virtual_memory + block;
Claus Gindharta7b35512008-04-28 17:51:09 +0000107
108 // clear status register
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000109 chip_writeb(0x50, bios);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000110 printf_debug("Erase at 0x%lx\n", bios);
Claus Gindharta7b35512008-04-28 17:51:09 +0000111 // now start it
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000112 chip_writeb(0x20, bios);
113 chip_writeb(0xd0, bios);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000114 programmer_delay(10);
Claus Gindharta7b35512008-04-28 17:51:09 +0000115
116 wait_stm50flw0x0x(flash->virtual_memory);
117
Sean Nelson56358aa2010-01-19 16:08:51 +0000118 if (check_erased_range(flash, block, blocksize)) {
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000119 fprintf(stderr, "ERASE FAILED!\n");
120 return -1;
Claus Gindharta7b35512008-04-28 17:51:09 +0000121 }
Sean Nelson56358aa2010-01-19 16:08:51 +0000122 printf("DONE BLOCK 0x%x\n", block);
123
124 return 0;
125}
126
127int erase_sector_stm50flw0x0x(struct flashchip *flash, unsigned int sector, unsigned int sectorsize)
128{
129 chipaddr bios = flash->virtual_memory + sector;
130
131 // clear status register
132 chip_writeb(0x50, bios);
133 printf_debug("Erase at 0x%lx\n", bios);
134 // now start it
135 chip_writeb(0x32, bios);
136 chip_writeb(0xd0, bios);
137 programmer_delay(10);
138
139 wait_stm50flw0x0x(flash->virtual_memory);
140
141 if (check_erased_range(flash, sector, sectorsize)) {
142 fprintf(stderr, "ERASE FAILED!\n");
143 return -1;
144 }
145 printf("DONE BLOCK 0x%x\n", sector);
Claus Gindharta7b35512008-04-28 17:51:09 +0000146
147 return 0;
148}
149
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000150int write_page_stm50flw0x0x(chipaddr bios, uint8_t *src,
151 chipaddr dst, int page_size)
Claus Gindharta7b35512008-04-28 17:51:09 +0000152{
Uwe Hermann394131e2008-10-18 21:14:13 +0000153 int i, rc = 0;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000154 chipaddr d = dst;
Claus Gindharta7b35512008-04-28 17:51:09 +0000155 uint8_t *s = src;
156
157 /* transfer data from source to destination */
158 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000159 chip_writeb(0x40, dst);
160 chip_writeb(*src++, dst++);
Claus Gindharta7b35512008-04-28 17:51:09 +0000161 wait_stm50flw0x0x(bios);
162 }
163
164/* claus.gindhart@kontron.com
165 * TODO
166 * I think, that verification is not required, but
167 * i leave it in anyway
168 */
169 dst = d;
170 src = s;
171 for (i = 0; i < page_size; i++) {
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000172 if (chip_readb(dst) != *src) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000173 rc = -1;
174 break;
175 }
176 dst++;
177 src++;
178 }
179
180 if (rc) {
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000181 fprintf(stderr, " page 0x%lx failed!\n",
182 (d - bios) / page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000183 }
184
185 return rc;
186}
187
188/* I simply erase block by block
189 * I Chip This is not the fastest way, but it works
190 */
191int erase_stm50flw0x0x(struct flashchip *flash)
192{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000193 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +0000194 int total_size = flash->total_size * 1024;
195 int page_size = flash->page_size;
Claus Gindharta7b35512008-04-28 17:51:09 +0000196
197 printf("Erasing page:\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000198 for (i = 0; i < total_size / page_size; i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000199 printf
200 ("\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");
201 printf("%04d at address: 0x%08x ", i, i * page_size);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000202 if (unlock_block_stm50flw0x0x(flash, i * page_size)) {
203 fprintf(stderr, "UNLOCK FAILED!\n");
204 return -1;
205 }
Sean Nelson56358aa2010-01-19 16:08:51 +0000206 if (erase_block_stm50flw0x0x(flash, i * page_size, page_size)) {
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000207 fprintf(stderr, "ERASE FAILED!\n");
208 return -1;
209 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000210 }
211 printf("\n");
Claus Gindharta7b35512008-04-28 17:51:09 +0000212
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000213 return 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000214}
215
Sean Nelson56358aa2010-01-19 16:08:51 +0000216int erase_chip_stm50flw0x0x(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
217{
218 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
219 msg_cerr("%s called with incorrect arguments\n",
220 __func__);
221 return -1;
222 }
223 return erase_stm50flw0x0x(flash);
224}
225
Claus Gindharta7b35512008-04-28 17:51:09 +0000226int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf)
227{
Uwe Hermann394131e2008-10-18 21:14:13 +0000228 int i, rc = 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000229 int total_size = flash->total_size * 1024;
230 int page_size = flash->page_size;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000231 chipaddr bios = flash->virtual_memory;
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000232 uint8_t *tmpbuf = malloc(page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000233
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000234 if (!tmpbuf) {
235 printf("Could not allocate memory!\n");
236 exit(1);
237 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000238 printf("Programming page: \n");
Uwe Hermann394131e2008-10-18 21:14:13 +0000239 for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000240 printf
241 ("\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");
242 printf("%04d at address: 0x%08x ", i, i * page_size);
243
244 /* Auto Skip Blocks, which already contain the desired data
245 * Faster, because we only write, what has changed
246 * More secure, because blocks, which are excluded
247 * (with the exclude or layout feature)
248 * are not erased and rewritten; data is retained also
249 * in sudden power off situations
250 */
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000251 chip_readn(tmpbuf, bios + i * page_size, page_size);
252 if (!memcmp((void *)(buf + i * page_size), tmpbuf, page_size)) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000253 printf("SKIPPED\n");
254 continue;
255 }
256
257 rc = unlock_block_stm50flw0x0x(flash, i * page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000258 if (!rc)
Sean Nelson56358aa2010-01-19 16:08:51 +0000259 rc = erase_block_stm50flw0x0x(flash, i * page_size, page_size);
Uwe Hermann394131e2008-10-18 21:14:13 +0000260 if (!rc)
261 write_page_stm50flw0x0x(bios, buf + i * page_size,
262 bios + i * page_size, page_size);
Claus Gindharta7b35512008-04-28 17:51:09 +0000263 }
264 printf("\n");
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000265 free(tmpbuf);
Claus Gindharta7b35512008-04-28 17:51:09 +0000266
267 return rc;
268}