blob: 1838efc6e92a88bd9424877f703ef3fb181eb33e [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
Claus Gindharta7b35512008-04-28 17:51:09 +000036/*
37 * claus.gindhart@kontron.com
38 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +000039 * before you can erase them or write to them.
40 */
Claus Gindharta7b35512008-04-28 17:51:09 +000041int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
42{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000043 chipaddr wrprotect = flash->virtual_registers + 2;
Claus Gindharta7b35512008-04-28 17:51:09 +000044 const uint8_t unlock_sector = 0x00;
45 int j;
46
Uwe Hermann394131e2008-10-18 21:14:13 +000047 /*
48 * These chips have to be unlocked before you can erase them or write
49 * to them. The size of the locking sectors depends on the type
50 * of chip.
51 *
52 * Sometimes, the BIOS does this for you; so you propably
53 * don't need to worry about that.
54 */
Claus Gindharta7b35512008-04-28 17:51:09 +000055
Uwe Hermann394131e2008-10-18 21:14:13 +000056 /* Check, if it's is a top/bottom-block with 4k-sectors. */
57 /* TODO: What about the other types? */
Claus Gindharta7b35512008-04-28 17:51:09 +000058 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +000059 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
60 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +000061
62 // unlock each 4k-sector
63 for (j = 0; j < 0x10000; j += 0x1000) {
64 printf_debug("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000065 chip_writeb(unlock_sector, wrprotect + offset + j);
66 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Uwe Hermann394131e2008-10-18 21:14:13 +000067 printf("Cannot unlock sector @ 0x%x\n",
68 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +000069 return -1;
70 }
71 }
72 } else {
73 printf_debug("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000074 chip_writeb(unlock_sector, wrprotect + offset);
75 if (chip_readb(wrprotect + offset) != unlock_sector) {
Claus Gindharta7b35512008-04-28 17:51:09 +000076 printf("Cannot unlock sector @ 0x%x\n", offset);
77 return -1;
78 }
79 }
80
81 return 0;
82}
83
Sean Nelson28accc22010-03-19 18:47:06 +000084int unlock_stm50flw0x0x(struct flashchip *flash)
Claus Gindharta7b35512008-04-28 17:51:09 +000085{
Sean Nelson28accc22010-03-19 18:47:06 +000086 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +000087
Sean Nelson28accc22010-03-19 18:47:06 +000088 for (i = 0; i < flash->total_size; i+= flash->page_size) {
89 if(unlock_block_stm50flw0x0x(flash, i)) {
90 fprintf(stderr, "UNLOCK FAILED!\n");
91 return -1;
92 }
Claus Gindharta7b35512008-04-28 17:51:09 +000093 }
Sean Nelson56358aa2010-01-19 16:08:51 +000094
95 return 0;
96}
97
98int erase_sector_stm50flw0x0x(struct flashchip *flash, unsigned int sector, unsigned int sectorsize)
99{
100 chipaddr bios = flash->virtual_memory + sector;
101
102 // clear status register
103 chip_writeb(0x50, bios);
104 printf_debug("Erase at 0x%lx\n", bios);
105 // now start it
106 chip_writeb(0x32, bios);
107 chip_writeb(0xd0, bios);
108 programmer_delay(10);
109
Sean Nelson28accc22010-03-19 18:47:06 +0000110 wait_82802ab(flash->virtual_memory);
Sean Nelson56358aa2010-01-19 16:08:51 +0000111
112 if (check_erased_range(flash, sector, sectorsize)) {
113 fprintf(stderr, "ERASE FAILED!\n");
114 return -1;
115 }
116 printf("DONE BLOCK 0x%x\n", sector);
Claus Gindharta7b35512008-04-28 17:51:09 +0000117
118 return 0;
119}
120
Sean Nelson28accc22010-03-19 18:47:06 +0000121int erase_chip_stm50flw0x0x(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Claus Gindharta7b35512008-04-28 17:51:09 +0000122{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000123 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +0000124 int total_size = flash->total_size * 1024;
125 int page_size = flash->page_size;
Claus Gindharta7b35512008-04-28 17:51:09 +0000126
Sean Nelson28accc22010-03-19 18:47:06 +0000127 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
128 msg_cerr("%s called with incorrect arguments\n",
129 __func__);
130 return -1;
131 }
132
Claus Gindharta7b35512008-04-28 17:51:09 +0000133 printf("Erasing page:\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000134 for (i = 0; i < total_size / page_size; i++) {
Claus Gindharta7b35512008-04-28 17:51:09 +0000135 printf
136 ("\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");
137 printf("%04d at address: 0x%08x ", i, i * page_size);
Sean Nelson28accc22010-03-19 18:47:06 +0000138 //if (unlock_block_stm50flw0x0x(flash, i * page_size)) {
139 // fprintf(stderr, "UNLOCK FAILED!\n");
140 // return -1;
141 //}
142 if (erase_block_82802ab(flash, i * page_size, page_size)) {
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000143 fprintf(stderr, "ERASE FAILED!\n");
144 return -1;
145 }
Claus Gindharta7b35512008-04-28 17:51:09 +0000146 }
147 printf("\n");
Claus Gindharta7b35512008-04-28 17:51:09 +0000148
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000149 return 0;
Claus Gindharta7b35512008-04-28 17:51:09 +0000150}