stm50flw0x0x.c 4.01 KB
Newer Older
1 2 3 4
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com>
5
 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

/*
 * This module is designed for supporting the devices
 * ST M50FLW040A (not yet tested)
 * ST M50FLW040B (not yet tested)
 * ST M50FLW080A
 * ST M50FLW080B (not yet tested)
 */

#include "flash.h"
31
#include "flashchips.h"
32
#include "chipdrivers.h"
33 34 35 36

/*
 * claus.gindhart@kontron.com
 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
37 38
 * before you can erase them or write to them.
 */
39
static int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
40
{
41
	chipaddr wrprotect = flash->virtual_registers + 2;
42 43 44
	const uint8_t unlock_sector = 0x00;
	int j;

45 46 47 48 49
	/*
	 * These chips have to be unlocked before you can erase them or write
	 * to them. The size of the locking sectors depends on the type
	 * of chip.
	 *
50
	 * Sometimes, the BIOS does this for you; so you probably
51 52 53 54 55
	 * don't need to worry about that.
	 */

	/* Check, if it's is a top/bottom-block with 4k-sectors. */
	/* TODO: What about the other types? */
56
	if ((offset == 0) ||
57 58
	    (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
	    || (offset == 0xF0000)) {
59 60 61

		// unlock each 4k-sector
		for (j = 0; j < 0x10000; j += 0x1000) {
62
			msg_cdbg("unlocking at 0x%x\n", offset + j);
63 64
			chip_writeb(unlock_sector, wrprotect + offset + j);
			if (chip_readb(wrprotect + offset + j) != unlock_sector) {
65
				msg_cerr("Cannot unlock sector @ 0x%x\n",
66
				       offset + j);
67 68 69 70
				return -1;
			}
		}
	} else {
71
		msg_cdbg("unlocking at 0x%x\n", offset);
72 73
		chip_writeb(unlock_sector, wrprotect + offset);
		if (chip_readb(wrprotect + offset) != unlock_sector) {
74
			msg_cerr("Cannot unlock sector @ 0x%x\n", offset);
75 76 77 78 79 80 81
			return -1;
		}
	}

	return 0;
}

82
int unlock_stm50flw0x0x(struct flashchip *flash)
83
{
84
	int i;
85

Sean Nelson's avatar
Sean Nelson committed
86
	for (i = 0; i < flash->total_size * 1024; i+= flash->page_size) {
87
		if(unlock_block_stm50flw0x0x(flash, i)) {
88
			msg_cerr("UNLOCK FAILED!\n");
89 90
			return -1;
		}
91
	}
92 93 94 95 96 97 98 99 100 101

	return 0;
}

int erase_sector_stm50flw0x0x(struct flashchip *flash, unsigned int sector, unsigned int sectorsize)
{
	chipaddr bios = flash->virtual_memory + sector;

	// clear status register
	chip_writeb(0x50, bios);
102
	msg_cdbg("Erase at 0x%lx\n", bios);
103 104 105 106 107
	// now start it
	chip_writeb(0x32, bios);
	chip_writeb(0xd0, bios);
	programmer_delay(10);

108
	wait_82802ab(flash->virtual_memory);
109 110

	if (check_erased_range(flash, sector, sectorsize)) {
111
		msg_cerr("ERASE FAILED!\n");
112 113
		return -1;
	}
114
	msg_cinfo("DONE BLOCK 0x%x\n", sector);
115 116 117 118

	return 0;
}

119
int erase_chip_stm50flw0x0x(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
120
{
121
	int i;
122 123 124
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;

125 126 127 128 129
	if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
		msg_cerr("%s called with incorrect arguments\n",
			__func__);
		return -1;
	}
130

131
	msg_cinfo("Erasing page:\n");
132
	for (i = 0; i < total_size / page_size; i++) {
133 134
		msg_cinfo("\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");
		msg_cinfo("%04d at address: 0x%08x ", i, i * page_size);
135
		//if (unlock_block_stm50flw0x0x(flash, i * page_size)) {
136
		//	msg_cerr("UNLOCK FAILED!\n");
137 138 139
		//	return -1;
		//}
		if (erase_block_82802ab(flash, i * page_size, page_size)) {
140
			msg_cerr("ERASE FAILED!\n");
141
			return -1;
142 143
		}
	}
144
	msg_cinfo("\n");
145

146
	return 0;
147
}