blob: f3fc4d8fe19bdbca3890df1c534dc0cd17d9e7a7 [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 Hailfingerad3cc552010-07-03 11:02:10 +000039static int unlock_block_stm50flw0x0x(struct flashchip *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 Hailfingerd13775e2009-05-11 20:04:30 +000063 chip_writeb(unlock_sector, wrprotect + offset + j);
64 if (chip_readb(wrprotect + offset + j) != unlock_sector) {
Sean Nelsoned479d22010-03-24 23:14:32 +000065 msg_cerr("Cannot unlock sector @ 0x%x\n",
Uwe Hermann394131e2008-10-18 21:14:13 +000066 offset + j);
Claus Gindharta7b35512008-04-28 17:51:09 +000067 return -1;
68 }
69 }
70 } else {
Sean Nelsoned479d22010-03-24 23:14:32 +000071 msg_cdbg("unlocking at 0x%x\n", offset);
Carl-Daniel Hailfingerd13775e2009-05-11 20:04:30 +000072 chip_writeb(unlock_sector, wrprotect + offset);
73 if (chip_readb(wrprotect + offset) != unlock_sector) {
Sean Nelsoned479d22010-03-24 23:14:32 +000074 msg_cerr("Cannot unlock sector @ 0x%x\n", offset);
Claus Gindharta7b35512008-04-28 17:51:09 +000075 return -1;
76 }
77 }
78
79 return 0;
80}
81
Sean Nelson28accc22010-03-19 18:47:06 +000082int unlock_stm50flw0x0x(struct flashchip *flash)
Claus Gindharta7b35512008-04-28 17:51:09 +000083{
Sean Nelson28accc22010-03-19 18:47:06 +000084 int i;
Claus Gindharta7b35512008-04-28 17:51:09 +000085
Sean Nelson46313192010-03-20 15:15:36 +000086 for (i = 0; i < flash->total_size * 1024; i+= flash->page_size) {
Sean Nelson28accc22010-03-19 18:47:06 +000087 if(unlock_block_stm50flw0x0x(flash, i)) {
Sean Nelsoned479d22010-03-24 23:14:32 +000088 msg_cerr("UNLOCK FAILED!\n");
Sean Nelson28accc22010-03-19 18:47:06 +000089 return -1;
90 }
Claus Gindharta7b35512008-04-28 17:51:09 +000091 }
Sean Nelson56358aa2010-01-19 16:08:51 +000092
93 return 0;
94}
95
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +000096/* This function is unused. */
Sean Nelson56358aa2010-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);
Sean Nelson56358aa2010-01-19 16:08:51 +0000103 // now start it
104 chip_writeb(0x32, bios);
105 chip_writeb(0xd0, bios);
106 programmer_delay(10);
107
Carl-Daniel Hailfingerb30a5ed2010-10-10 14:02:27 +0000108 wait_82802ab(flash);
Sean Nelson56358aa2010-01-19 16:08:51 +0000109
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000110 /* FIXME: Check the status register for errors. */
Claus Gindharta7b35512008-04-28 17:51:09 +0000111 return 0;
112}