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 | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 39 | static int unlock_block_stm50flw0x0x(struct flashchip *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) || |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 57 | (offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000)) |
| 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 | d13775e | 2009-05-11 20:04:30 +0000 | [diff] [blame] | 63 | chip_writeb(unlock_sector, wrprotect + offset + j); |
| 64 | if (chip_readb(wrprotect + offset + j) != unlock_sector) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 65 | msg_cerr("Cannot unlock sector @ 0x%x\n", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 66 | offset + j); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | } |
| 70 | } else { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 71 | msg_cdbg("unlocking at 0x%x\n", offset); |
Carl-Daniel Hailfinger | d13775e | 2009-05-11 20:04:30 +0000 | [diff] [blame] | 72 | chip_writeb(unlock_sector, wrprotect + offset); |
| 73 | if (chip_readb(wrprotect + offset) != unlock_sector) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 74 | msg_cerr("Cannot unlock sector @ 0x%x\n", offset); |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 82 | int unlock_stm50flw0x0x(struct flashchip *flash) |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 83 | { |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 84 | int i; |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 85 | |
Sean Nelson | 4631319 | 2010-03-20 15:15:36 +0000 | [diff] [blame] | 86 | for (i = 0; i < flash->total_size * 1024; i+= flash->page_size) { |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 87 | if(unlock_block_stm50flw0x0x(flash, i)) { |
Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 88 | msg_cerr("UNLOCK FAILED!\n"); |
Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 89 | return -1; |
| 90 | } |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 91 | } |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Carl-Daniel Hailfinger | f52f784 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 96 | /* This function is unused. */ |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 97 | int 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 Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 103 | // now start it |
| 104 | chip_writeb(0x32, bios); |
| 105 | chip_writeb(0xd0, bios); |
| 106 | programmer_delay(10); |
| 107 | |
Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 108 | wait_82802ab(flash); |
Sean Nelson | 56358aa | 2010-01-19 16:08:51 +0000 | [diff] [blame] | 109 | |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 110 | /* FIXME: Check the status register for errors. */ |
Claus Gindhart | a7b3551 | 2008-04-28 17:51:09 +0000 | [diff] [blame] | 111 | return 0; |
| 112 | } |