Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com> |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com> |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 6 | * |
| 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 Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 20 | */ |
| 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 Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 30 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 31 | #include "flashchips.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 32 | #include "chipdrivers.h" |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 33 | |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 34 | /* |
| 35 | * claus.gindhart@kontron.com |
| 36 | * The ST M50FLW080B and STM50FLW080B chips have to be unlocked, |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 37 | * before you can erase them or write to them. |
| 38 | */ |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 39 | static int unlock_block_stm50flw0x0x(struct flashctx *flash, int offset) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 40 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 41 | chipaddr wrprotect = flash->virtual_registers + 2; |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 42 | static const uint8_t unlock_sector = 0x00; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 43 | int j; |
| 44 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 45 | /* |
| 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 Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 50 | * Sometimes, the BIOS does this for you; so you probably |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 51 | * don't need to worry about that. |
| 52 | */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 53 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 54 | /* Check, if it's is a top/bottom-block with 4k-sectors. */ |
| 55 | /* TODO: What about the other types? */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 56 | if ((offset == 0) || |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 57 | (offset == (flash->chip->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000)) |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 58 | || (offset == 0xF0000)) { |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 59 | |
| 60 | // unlock each 4k-sector |
| 61 | for (j = 0; j < 0x10000; j += 0x1000) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 62 | msg_cdbg("unlocking at 0x%x\n", offset + j); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 63 | chip_writeb(flash, unlock_sector, |
| 64 | wrprotect + offset + j); |
| 65 | if (chip_readb(flash, wrprotect + offset + j) != |
| 66 | unlock_sector) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 67 | msg_cerr("Cannot unlock sector @ 0x%x\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 68 | offset + j); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | } |
| 72 | } else { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 73 | msg_cdbg("unlocking at 0x%x\n", offset); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 74 | chip_writeb(flash, unlock_sector, wrprotect + offset); |
| 75 | if (chip_readb(flash, wrprotect + offset) != unlock_sector) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 76 | msg_cerr("Cannot unlock sector @ 0x%x\n", offset); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 77 | return -1; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 84 | int unlock_stm50flw0x0x(struct flashctx *flash) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 85 | { |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 86 | int i; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 87 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 88 | for (i = 0; i < flash->chip->total_size * 1024; i+= flash->chip->page_size) { |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 89 | if(unlock_block_stm50flw0x0x(flash, i)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 90 | msg_cerr("UNLOCK FAILED!\n"); |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 91 | return -1; |
| 92 | } |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 93 | } |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Carl-Daniel Hailfinger | f52f784 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 98 | /* This function is unused. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 99 | int erase_sector_stm50flw0x0x(struct flashctx *flash, unsigned int sector, |
| 100 | unsigned int sectorsize) |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 101 | { |
| 102 | chipaddr bios = flash->virtual_memory + sector; |
| 103 | |
| 104 | // clear status register |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 105 | chip_writeb(flash, 0x50, bios); |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 106 | // now start it |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 107 | chip_writeb(flash, 0x32, bios); |
| 108 | chip_writeb(flash, 0xd0, bios); |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 109 | programmer_delay(10); |
| 110 | |
Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 111 | wait_82802ab(flash); |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 112 | |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 113 | /* FIXME: Check the status register for errors. */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |