blob: 60e5c45283c3a28d52f8db4360f6c83fc72a1b96 [file] [log] [blame]
stepan92251692008-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>
snelsone0c56352010-01-19 16:08:51 +00005 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
stepan92251692008-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
stepan92251692008-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)
stepan92251692008-04-28 17:51:09 +000028 */
29
stepan92251692008-04-28 17:51:09 +000030#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000031#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000032#include "chipdrivers.h"
stepan92251692008-04-28 17:51:09 +000033
stepan92251692008-04-28 17:51:09 +000034/*
35 * claus.gindhart@kontron.com
36 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
uwefa98ca12008-10-18 21:14:13 +000037 * before you can erase them or write to them.
38 */
David Hendricks82fd8ae2010-08-04 14:34:54 -070039static int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
stepan92251692008-04-28 17:51:09 +000040{
hailfinger82719632009-05-16 21:22:56 +000041 chipaddr wrprotect = flash->virtual_registers + 2;
David Hendricks668f29d2011-01-27 18:51:45 -080042 static const uint8_t unlock_sector = 0x00;
stepan92251692008-04-28 17:51:09 +000043 int j;
44
uwefa98ca12008-10-18 21:14:13 +000045 /*
46 * These chips have to be unlocked before you can erase them or write
47 * to them. The size of the locking sectors depends on the type
48 * of chip.
49 *
uwe3a3ab2f2010-03-25 23:18:41 +000050 * Sometimes, the BIOS does this for you; so you probably
uwefa98ca12008-10-18 21:14:13 +000051 * don't need to worry about that.
52 */
stepan92251692008-04-28 17:51:09 +000053
uwefa98ca12008-10-18 21:14:13 +000054 /* Check, if it's is a top/bottom-block with 4k-sectors. */
55 /* TODO: What about the other types? */
stepan92251692008-04-28 17:51:09 +000056 if ((offset == 0) ||
uwefa98ca12008-10-18 21:14:13 +000057 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
58 || (offset == 0xF0000)) {
stepan92251692008-04-28 17:51:09 +000059
60 // unlock each 4k-sector
61 for (j = 0; j < 0x10000; j += 0x1000) {
snelsonfc007bb2010-03-24 23:14:32 +000062 msg_cdbg("unlocking at 0x%x\n", offset + j);
hailfinger678021d2009-05-11 20:04:30 +000063 chip_writeb(unlock_sector, wrprotect + offset + j);
64 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
snelsonfc007bb2010-03-24 23:14:32 +000065 msg_cerr("Cannot unlock sector @ 0x%x\n",
uwefa98ca12008-10-18 21:14:13 +000066 offset + j);
stepan92251692008-04-28 17:51:09 +000067 return -1;
68 }
69 }
70 } else {
snelsonfc007bb2010-03-24 23:14:32 +000071 msg_cdbg("unlocking at 0x%x\n", offset);
hailfinger678021d2009-05-11 20:04:30 +000072 chip_writeb(unlock_sector, wrprotect + offset);
73 if (chip_readb(wrprotect + offset) != unlock_sector) {
snelsonfc007bb2010-03-24 23:14:32 +000074 msg_cerr("Cannot unlock sector @ 0x%x\n", offset);
stepan92251692008-04-28 17:51:09 +000075 return -1;
76 }
77 }
78
79 return 0;
80}
81
snelsonc0acbeb2010-03-19 18:47:06 +000082int unlock_stm50flw0x0x(struct flashchip *flash)
stepan92251692008-04-28 17:51:09 +000083{
snelsonc0acbeb2010-03-19 18:47:06 +000084 int i;
stepan92251692008-04-28 17:51:09 +000085
snelson7af202b2010-03-20 15:15:36 +000086 for (i = 0; i < flash->total_size * 1024; i+= flash->page_size) {
snelsonc0acbeb2010-03-19 18:47:06 +000087 if(unlock_block_stm50flw0x0x(flash, i)) {
snelsonfc007bb2010-03-24 23:14:32 +000088 msg_cerr("UNLOCK FAILED!\n");
snelsonc0acbeb2010-03-19 18:47:06 +000089 return -1;
90 }
stepan92251692008-04-28 17:51:09 +000091 }
snelsone0c56352010-01-19 16:08:51 +000092
93 return 0;
94}
95
David Hendricksc801adb2010-12-09 16:58:56 -080096/* This function is unused. */
snelsone0c56352010-01-19 16:08:51 +000097int erase_sector_stm50flw0x0x(struct flashchip *flash, unsigned int sector, unsigned int sectorsize)
98{
99 chipaddr bios = flash->virtual_memory + sector;
100
101 // clear status register
102 chip_writeb(0x50, bios);
snelsone0c56352010-01-19 16:08:51 +0000103 // now start it
104 chip_writeb(0x32, bios);
105 chip_writeb(0xd0, bios);
106 programmer_delay(10);
107
David Hendricksc801adb2010-12-09 16:58:56 -0800108 wait_82802ab(flash);
snelsone0c56352010-01-19 16:08:51 +0000109
110 if (check_erased_range(flash, sector, sectorsize)) {
snelsonfc007bb2010-03-24 23:14:32 +0000111 msg_cerr("ERASE FAILED!\n");
snelsone0c56352010-01-19 16:08:51 +0000112 return -1;
113 }
stepan92251692008-04-28 17:51:09 +0000114
115 return 0;
116}
117
David Hendricksc801adb2010-12-09 16:58:56 -0800118/* FIXME: This function is not a real chip erase function. */
snelsonc0acbeb2010-03-19 18:47:06 +0000119int erase_chip_stm50flw0x0x(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
stepan92251692008-04-28 17:51:09 +0000120{
hailfinger7af83692009-06-15 17:23:36 +0000121 int i;
stepan92251692008-04-28 17:51:09 +0000122 int total_size = flash->total_size * 1024;
123 int page_size = flash->page_size;
stepan92251692008-04-28 17:51:09 +0000124
snelsonc0acbeb2010-03-19 18:47:06 +0000125 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
126 msg_cerr("%s called with incorrect arguments\n",
127 __func__);
128 return -1;
129 }
130
hailfinger7af83692009-06-15 17:23:36 +0000131 for (i = 0; i < total_size / page_size; i++) {
snelsonc0acbeb2010-03-19 18:47:06 +0000132 //if (unlock_block_stm50flw0x0x(flash, i * page_size)) {
snelsonfc007bb2010-03-24 23:14:32 +0000133 // msg_cerr("UNLOCK FAILED!\n");
snelsonc0acbeb2010-03-19 18:47:06 +0000134 // return -1;
135 //}
136 if (erase_block_82802ab(flash, i * page_size, page_size)) {
snelsonfc007bb2010-03-24 23:14:32 +0000137 msg_cerr("ERASE FAILED!\n");
hailfinger7af83692009-06-15 17:23:36 +0000138 return -1;
139 }
stepan92251692008-04-28 17:51:09 +0000140 }
stepan92251692008-04-28 17:51:09 +0000141
hailfinger7af83692009-06-15 17:23:36 +0000142 return 0;
stepan92251692008-04-28 17:51:09 +0000143}