blob: 9b6443e5093c5c2cd54fb402ba9006b8b545535b [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 "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000031#include "flashchips.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000032#include "chipdrivers.h"
Claus Gindharta7b35512008-04-28 17:51:09 +000033
Claus Gindharta7b35512008-04-28 17:51:09 +000034/*
35 * claus.gindhart@kontron.com
36 * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
Uwe Hermann394131e2008-10-18 21:14:13 +000037 * before you can erase them or write to them.
38 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000039static int unlock_block_stm50flw0x0x(struct flashctx *flash, int offset)
Claus Gindharta7b35512008-04-28 17:51:09 +000040{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000041 chipaddr wrprotect = flash->virtual_registers + 2;
Mathias Krausea60faab2011-01-17 07:50:42 +000042 static const uint8_t unlock_sector = 0x00;
Claus Gindharta7b35512008-04-28 17:51:09 +000043 int j;
44
Uwe Hermann394131e2008-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 *
Uwe Hermann4e3d0b32010-03-25 23:18:41 +000050 * Sometimes, the BIOS does this for you; so you probably
Uwe Hermann394131e2008-10-18 21:14:13 +000051 * don't need to worry about that.
52 */
Claus Gindharta7b35512008-04-28 17:51:09 +000053
Uwe Hermann394131e2008-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? */
Claus Gindharta7b35512008-04-28 17:51:09 +000056 if ((offset == 0) ||
Uwe Hermann394131e2008-10-18 21:14:13 +000057 (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
58 || (offset == 0xF0000)) {
Claus Gindharta7b35512008-04-28 17:51:09 +000059
60 // unlock each 4k-sector
61 for (j = 0; j < 0x10000; j += 0x1000) {
Sean Nelsoned479d22010-03-24 23:14:32 +000062 msg_cdbg("unlocking at 0x%x\n", offset + j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000063 chip_writeb(flash, unlock_sector,
64 wrprotect + offset + j);
65 if (chip_readb(flash, wrprotect + offset + j) !=
66 unlock_sector) {
Sean Nelsoned479d22010-03-24 23:14:32 +000067 msg_cerr("Cannot unlock sector @ 0x%x\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000068 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +000069 return -1;
70 }
71 }
72 } else {
Sean Nelsoned479d22010-03-24 23:14:32 +000073 msg_cdbg("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000074 chip_writeb(flash, unlock_sector, wrprotect + offset);
75 if (chip_readb(flash, wrprotect + offset) != unlock_sector) {
Sean Nelsoned479d22010-03-24 23:14:32 +000076 msg_cerr("Cannot unlock sector @ 0x%x\n", offset);
Claus Gindharta7b35512008-04-28 17:51:09 +000077 return -1;
78 }
79 }
80
81 return 0;
82}
83
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000084int unlock_stm50flw0x0x(struct flashctx *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 Nelson46313192010-03-20 15:15:36 +000088 for (i = 0; i < flash->total_size * 1024; i+= flash->page_size) {
Sean Nelson28accc22010-03-19 18:47:06 +000089 if(unlock_block_stm50flw0x0x(flash, i)) {
Sean Nelsoned479d22010-03-24 23:14:32 +000090 msg_cerr("UNLOCK FAILED!\n");
Sean Nelson28accc22010-03-19 18:47:06 +000091 return -1;
92 }
Claus Gindharta7b35512008-04-28 17:51:09 +000093 }
Sean Nelson56358aa2010-01-19 16:08:51 +000094
95 return 0;
96}
97
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +000098/* This function is unused. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000099int erase_sector_stm50flw0x0x(struct flashctx *flash, unsigned int sector,
100 unsigned int sectorsize)
Sean Nelson56358aa2010-01-19 16:08:51 +0000101{
102 chipaddr bios = flash->virtual_memory + sector;
103
104 // clear status register
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000105 chip_writeb(flash, 0x50, bios);
Sean Nelson56358aa2010-01-19 16:08:51 +0000106 // now start it
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000107 chip_writeb(flash, 0x32, bios);
108 chip_writeb(flash, 0xd0, bios);
Sean Nelson56358aa2010-01-19 16:08:51 +0000109 programmer_delay(10);
110
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000111 wait_82802ab(flash);
Sean Nelson56358aa2010-01-19 16:08:51 +0000112
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000113 /* FIXME: Check the status register for errors. */
Claus Gindharta7b35512008-04-28 17:51:09 +0000114 return 0;
115}