blob: 26bd6ed918b99e8397555d1b2a1d64cb7dc53c40 [file] [log] [blame]
Stefan Tauner4404f732013-09-12 08:28:56 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com>
5 * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
6 * Copyright (C) 2013 Stefan Tauner
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/*
24 * All ST M50 chips are locked on startup. Most of them have a uniform 64 kB block layout, but some have
25 * a non-uniform block/sector segmentation which has to be handled with more care. Some of the non-uniform
26 * chips support erasing of the 4 kB sectors with another command.
27 */
28
29#include "flash.h"
30#include "flashchips.h"
31#include "chipdrivers.h"
32
33static int stm50_unlock_address(struct flashctx *flash, int offset)
34{
35 chipaddr wrprotect = flash->virtual_registers + 2;
36 static const uint8_t unlock_sector = 0x00;
37 msg_cdbg("unlocking at 0x%x\n", offset);
38 chip_writeb(flash, unlock_sector, wrprotect + offset);
39 if (chip_readb(flash, wrprotect + offset) != unlock_sector) {
40 msg_cerr("Cannot unlock address 0x%x\n", offset);
41 return -1;
42 }
43 return 0;
44}
45
46/* Chips known to use a non-uniform block and sector layout for locking (as well as for erasing):
47 * Name Size Address range of lock registers
48 * M50FLW080A 1MB FFB00002 - FFBFF002
49 * M50FLW080B 1MB FFB00002 - FFBFF002
50 * M50FW002 256k FFBC0002 - FFBFC002
51 * M50LPW116 2MB FFA00002 - FFBFC002
52 */
53int unlock_stm50_nonuniform(struct flashctx *flash)
54{
55 int i;
56 struct eraseblock *eraseblocks = flash->chip->block_erasers[0].eraseblocks;
57 unsigned int done = 0;
58 for (i = 0; i < NUM_ERASEREGIONS && eraseblocks[i].count != 0; i++) {
59 unsigned int block_size = eraseblocks[i].size;
60 unsigned int block_count = eraseblocks[i].count;
61
62 int j;
63 for (j = 0; j < block_count; j++) {
64 if (stm50_unlock_address(flash, done)) {
65 msg_cerr("UNLOCK FAILED!\n");
66 return -1;
67 }
68 done += block_count * block_size;
69 }
70 }
71 return 0;
72}
73
74/* Unlocking for uniform 64 kB blocks starting at offset 2 of the feature registers. */
75int unlock_stm50_uniform(struct flashctx *flash)
76{
77 int i;
78 for (i = 0; i < flash->chip->total_size * 1024; i+= 64 * 1024) {
79 if (stm50_unlock_address(flash, i)) {
80 msg_cerr("UNLOCK FAILED!\n");
81 return -1;
82 }
83 }
84 return 0;
85}
86
87/* This function is unused. */
88int erase_sector_stm50(struct flashctx *flash, unsigned int sector, unsigned int sectorsize)
89{
90 chipaddr bios = flash->virtual_memory + sector;
91
92 // clear status register
93 chip_writeb(flash, 0x50, bios);
94 // now start it
95 chip_writeb(flash, 0x32, bios);
96 chip_writeb(flash, 0xd0, bios);
97 programmer_delay(10);
98
99 wait_82802ab(flash);
100
101 /* FIXME: Check the status register for errors. */
102 return 0;
103}