blob: b349b0e47ab5213af277dc01ccc6e8f62344cfaf [file] [log] [blame]
stepan5c3f1382007-02-06 19:47:50 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
stepan5c3f1382007-02-06 19:47:50 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2000 Ronald G. Minnich <rminnich@gmail.com>
stepan6d42c0f2009-08-12 09:27:45 +00006 * Copyright (C) 2005-2009 coresystems GmbH
hailfinger77c5d932009-06-15 12:10:57 +00007 * Copyright (C) 2006-2009 Carl-Daniel Hailfinger
stepan5c3f1382007-02-06 19:47:50 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
stepan5c3f1382007-02-06 19:47:50 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
stepan5c3f1382007-02-06 19:47:50 +000018 *
uweb25f1ea2007-08-29 17:52:32 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
stepan5c3f1382007-02-06 19:47:50 +000022 */
23
rminnich8d3ff912003-10-25 17:01:29 +000024#ifndef __FLASH_H__
25#define __FLASH_H__ 1
26
ollie6a600992005-11-26 21:55:36 +000027#include <stdint.h>
hailfingerd43a4e32010-06-03 00:49:50 +000028#include <stddef.h>
hailfinger088dc812009-12-14 03:32:24 +000029#include "hwaccess.h"
oxygene3ad3b332010-01-06 22:14:39 +000030#ifdef _WIN32
31#include <windows.h>
32#undef min
33#undef max
34#endif
hailfingere1f062f2008-05-22 13:22:45 +000035
Stefan Reinauere64faaf2011-05-03 18:03:25 -070036/* Are timers broken? */
37extern int broken_timer;
38
David Hendricksbf36f092010-11-02 23:39:29 -070039struct flashchip; /* forward declare */
hailfingerf294fa22010-09-25 22:53:44 +000040#define ERROR_PTR ((void*)-1)
41
hailfingeree9ee132010-10-08 00:37:55 +000042/* Error codes */
43#define TIMEOUT_ERROR -101
44
hailfinger82719632009-05-16 21:22:56 +000045typedef unsigned long chipaddr;
46
dhendrix0ffc2eb2011-06-14 01:35:36 +000047int register_shutdown(int (*function) (void *data), void *data);
David Hendricksbf36f092010-11-02 23:39:29 -070048#define CHIP_RESTORE_CALLBACK int (*func) (struct flashchip *flash, uint8_t status)
49
David Hendricksbf36f092010-11-02 23:39:29 -070050int register_chip_restore(CHIP_RESTORE_CALLBACK, struct flashchip *flash, uint8_t status);
uweabe92a52009-05-16 22:36:00 +000051void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
52 size_t len);
53void programmer_unmap_flash_region(void *virt_addr, size_t len);
54void chip_writeb(uint8_t val, chipaddr addr);
55void chip_writew(uint16_t val, chipaddr addr);
56void chip_writel(uint32_t val, chipaddr addr);
hailfinger9d987ef2009-06-05 18:32:07 +000057void chip_writen(uint8_t *buf, chipaddr addr, size_t len);
uweabe92a52009-05-16 22:36:00 +000058uint8_t chip_readb(const chipaddr addr);
59uint16_t chip_readw(const chipaddr addr);
60uint32_t chip_readl(const chipaddr addr);
hailfinger9d987ef2009-06-05 18:32:07 +000061void chip_readn(uint8_t *buf, const chipaddr addr, size_t len);
hailfingere5829f62009-06-05 17:48:08 +000062void programmer_delay(int usecs);
hailfingerba3761a2009-03-05 19:24:22 +000063
uwe16f99092008-03-12 11:54:51 +000064#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
65
hailfinger40167462009-05-31 17:57:34 +000066enum chipbustype {
hailfinger668f3502009-06-01 00:02:11 +000067 CHIP_BUSTYPE_NONE = 0,
hailfinger40167462009-05-31 17:57:34 +000068 CHIP_BUSTYPE_PARALLEL = 1 << 0,
69 CHIP_BUSTYPE_LPC = 1 << 1,
70 CHIP_BUSTYPE_FWH = 1 << 2,
71 CHIP_BUSTYPE_SPI = 1 << 3,
72 CHIP_BUSTYPE_NONSPI = CHIP_BUSTYPE_PARALLEL | CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH,
73 CHIP_BUSTYPE_UNKNOWN = CHIP_BUSTYPE_PARALLEL | CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH | CHIP_BUSTYPE_SPI,
74};
75
David Hendricks80f62d22010-10-08 11:09:35 -070076/* used to select bus which target chip resides */
77extern enum chipbustype target_bus;
78
hailfinger7df21362009-09-05 02:30:58 +000079/*
80 * How many different contiguous runs of erase blocks with one size each do
81 * we have for a given erase function?
82 */
83#define NUM_ERASEREGIONS 5
84
85/*
86 * How many different erase functions do we have per chip?
hailfingerc33d4732010-07-29 13:09:18 +000087 * Atmel AT25FS010 has 6 different functions.
hailfinger7df21362009-09-05 02:30:58 +000088 */
hailfingerc33d4732010-07-29 13:09:18 +000089#define NUM_ERASEFUNCTIONS 6
hailfinger7df21362009-09-05 02:30:58 +000090
hailfinger80dea312010-01-09 03:15:50 +000091#define FEATURE_REGISTERMAP (1 << 0)
92#define FEATURE_BYTEWRITES (1 << 1)
snelsonc6855342010-01-28 23:55:12 +000093#define FEATURE_LONG_RESET (0 << 4)
94#define FEATURE_SHORT_RESET (1 << 4)
95#define FEATURE_EITHER_RESET FEATURE_LONG_RESET
hailfingerb07dc972010-10-20 21:13:19 +000096#define FEATURE_RESET_MASK (FEATURE_LONG_RESET | FEATURE_SHORT_RESET)
hailfinger80dea312010-01-09 03:15:50 +000097#define FEATURE_ADDR_FULL (0 << 2)
98#define FEATURE_ADDR_MASK (3 << 2)
snelsonc6855342010-01-28 23:55:12 +000099#define FEATURE_ADDR_2AA (1 << 2)
100#define FEATURE_ADDR_AAA (2 << 2)
mkarcher9ded5fe2010-04-03 10:27:08 +0000101#define FEATURE_ADDR_SHIFTED (1 << 5)
hailfingerc33d4732010-07-29 13:09:18 +0000102#define FEATURE_WRSR_EWSR (1 << 6)
103#define FEATURE_WRSR_WREN (1 << 7)
104#define FEATURE_WRSR_EITHER (FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN)
snelson63133f92010-01-04 17:15:23 +0000105
rminnich8d3ff912003-10-25 17:01:29 +0000106struct flashchip {
uwedfcd15f2008-03-14 23:55:58 +0000107 const char *vendor;
uwe6ed6d952007-12-04 21:49:06 +0000108 const char *name;
hailfinger40167462009-05-31 17:57:34 +0000109
110 enum chipbustype bustype;
111
uwefa98ca12008-10-18 21:14:13 +0000112 /*
113 * With 32bit manufacture_id and model_id we can cover IDs up to
hailfinger428f2012007-12-31 01:49:00 +0000114 * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's
115 * Identification code.
116 */
117 uint32_t manufacture_id;
118 uint32_t model_id;
rminnich8d3ff912003-10-25 17:01:29 +0000119
stefanct707f13b2011-05-19 02:58:17 +0000120 /* Total chip size in kilobytes */
rminnich8d3ff912003-10-25 17:01:29 +0000121 int total_size;
stefanct707f13b2011-05-19 02:58:17 +0000122 /* Chip page size in bytes */
rminnich8d3ff912003-10-25 17:01:29 +0000123 int page_size;
snelson63133f92010-01-04 17:15:23 +0000124 int feature_bits;
rminnich8d3ff912003-10-25 17:01:29 +0000125
uwefa98ca12008-10-18 21:14:13 +0000126 /*
127 * Indicate if flashrom has been tested with this flash chip and if
stuge9cd64bd2008-05-03 04:34:37 +0000128 * everything worked correctly.
129 */
130 uint32_t tested;
131
uwe8e1a2ba2007-04-01 19:44:21 +0000132 int (*probe) (struct flashchip *flash);
hailfingerd5b35922009-06-03 14:46:22 +0000133
134 /* Delay after "enter/exit ID mode" commands in microseconds. */
135 int probe_timing;
hailfinger7df21362009-09-05 02:30:58 +0000136
137 /*
hailfingerc4fac582009-12-22 13:04:53 +0000138 * Erase blocks and associated erase function. Any chip erase function
139 * is stored as chip-sized virtual block together with said function.
stefanct707f13b2011-05-19 02:58:17 +0000140 * The first one that fits will be chosen. There is currently no way to
141 * influence that behaviour. For testing just comment out the other
142 * elements or set the function pointer to NULL.
hailfinger7df21362009-09-05 02:30:58 +0000143 */
144 struct block_eraser {
145 struct eraseblock{
stefanct312d9ff2011-06-12 19:47:55 +0000146 unsigned int size; /* Eraseblock size in bytes */
hailfinger7df21362009-09-05 02:30:58 +0000147 unsigned int count; /* Number of contiguous blocks with that size */
148 } eraseblocks[NUM_ERASEREGIONS];
stefanct9e6b98a2011-05-28 02:37:14 +0000149 /* a block_erase function should try to erase one block of size
150 * 'blocklen' at address 'blockaddr' and return 0 on success. */
hailfinger7df21362009-09-05 02:30:58 +0000151 int (*block_erase) (struct flashchip *flash, unsigned int blockaddr, unsigned int blocklen);
152 } block_erasers[NUM_ERASEFUNCTIONS];
153
snelson1ee293c2010-02-19 00:52:10 +0000154 int (*printlock) (struct flashchip *flash);
155 int (*unlock) (struct flashchip *flash);
hailfinger71e1bd42010-10-13 22:26:56 +0000156 int (*write) (struct flashchip *flash, uint8_t *buf, int start, int len);
hailfinger0f08b7a2009-06-16 08:55:44 +0000157 int (*read) (struct flashchip *flash, uint8_t *buf, int start, int len);
stefanct7e00e222011-06-03 07:26:31 +0000158 struct {
159 uint16_t min;
160 uint16_t max;
161 } voltage;
rminnich8d3ff912003-10-25 17:01:29 +0000162
uwe6ed6d952007-12-04 21:49:06 +0000163 /* Some flash devices have an additional register space. */
hailfinger82719632009-05-16 21:22:56 +0000164 chipaddr virtual_memory;
165 chipaddr virtual_registers;
David Hendricksf7924d12010-06-10 21:26:44 -0700166
167 struct wp *wp;
rminnich8d3ff912003-10-25 17:01:29 +0000168};
169
stuge9cd64bd2008-05-03 04:34:37 +0000170#define TEST_UNTESTED 0
171
uwe4e204a22009-05-28 15:07:42 +0000172#define TEST_OK_PROBE (1 << 0)
173#define TEST_OK_READ (1 << 1)
174#define TEST_OK_ERASE (1 << 2)
175#define TEST_OK_WRITE (1 << 3)
176#define TEST_OK_PR (TEST_OK_PROBE | TEST_OK_READ)
177#define TEST_OK_PRE (TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE)
hailfinger80f48682009-09-23 22:01:33 +0000178#define TEST_OK_PRW (TEST_OK_PROBE | TEST_OK_READ | TEST_OK_WRITE)
uwe4e204a22009-05-28 15:07:42 +0000179#define TEST_OK_PREW (TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE | TEST_OK_WRITE)
stuge9cd64bd2008-05-03 04:34:37 +0000180#define TEST_OK_MASK 0x0f
181
uwe4e204a22009-05-28 15:07:42 +0000182#define TEST_BAD_PROBE (1 << 4)
183#define TEST_BAD_READ (1 << 5)
184#define TEST_BAD_ERASE (1 << 6)
185#define TEST_BAD_WRITE (1 << 7)
186#define TEST_BAD_PREW (TEST_BAD_PROBE | TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE)
stuge9cd64bd2008-05-03 04:34:37 +0000187#define TEST_BAD_MASK 0xf0
188
hailfingerd5b35922009-06-03 14:46:22 +0000189/* Timing used in probe routines. ZERO is -2 to differentiate between an unset
190 * field and zero delay.
191 *
192 * SPI devices will always have zero delay and ignore this field.
193 */
194#define TIMING_FIXME -1
195/* this is intentionally same value as fixme */
196#define TIMING_IGNORED -1
197#define TIMING_ZERO -2
198
hailfinger48ed3e22011-05-04 00:39:50 +0000199extern const struct flashchip flashchips[];
ollie6a600992005-11-26 21:55:36 +0000200
uwe884cc8b2009-06-17 12:07:12 +0000201/* print.c */
202char *flashbuses_to_text(enum chipbustype bustype);
hailfingera50d60e2009-11-17 09:57:34 +0000203void print_supported(void);
hailfingera50d60e2009-11-17 09:57:34 +0000204void print_supported_wiki(void);
uwea3a82c92009-05-15 17:02:34 +0000205
uwe4529d202007-08-23 13:34:59 +0000206/* flashrom.c */
hailfingerb247c7a2010-03-08 00:42:32 +0000207enum write_granularity {
208 write_gran_1bit,
209 write_gran_1byte,
210 write_gran_256bytes,
211};
hailfinger80422e22009-12-13 22:28:00 +0000212extern enum chipbustype buses_supported;
uwee06bcf82009-04-24 16:17:41 +0000213extern int verbose;
krause2eb76212011-01-17 07:50:42 +0000214extern const char flashrom_version[];
hailfinger92cd8e32010-01-07 03:24:05 +0000215extern char *chip_to_probe;
stuge5ff0e6c2009-01-26 00:39:57 +0000216void map_flash_registers(struct flashchip *flash);
hailfinger0f08b7a2009-06-16 08:55:44 +0000217int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len);
hailfinger7df21362009-09-05 02:30:58 +0000218int erase_flash(struct flashchip *flash);
hailfinger48ed3e22011-05-04 00:39:50 +0000219int probe_flash(int startchip, struct flashchip *fill_flash, int force);
stefanct52700282011-06-26 17:38:17 +0000220int read_flash_to_file(struct flashchip *flash, const char *filename);
hailfinger7b414742009-06-13 12:04:03 +0000221int min(int a, int b);
hailfinger7af83692009-06-15 17:23:36 +0000222int max(int a, int b);
hailfingerf76cc322010-11-09 22:00:31 +0000223void tolower_string(char *str);
stefanct52700282011-06-26 17:38:17 +0000224char *extract_param(char **haystack, const char *needle, const char *delim);
225int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, const char *message);
hailfingerb247c7a2010-03-08 00:42:32 +0000226int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran);
uwe884cc8b2009-06-17 12:07:12 +0000227char *strcat_realloc(char *dest, const char *src);
hailfinger92cd8e32010-01-07 03:24:05 +0000228void print_version(void);
hailfinger74819ad2010-05-15 15:04:37 +0000229void print_banner(void);
hailfingerf79d1712010-10-06 23:48:34 +0000230void list_programmers_linebreak(int startcol, int cols, int paren);
hailfinger92cd8e32010-01-07 03:24:05 +0000231int selfcheck(void);
stefanct52700282011-06-26 17:38:17 +0000232int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it);
233int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);
234int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filename);
uwe884cc8b2009-06-17 12:07:12 +0000235
236#define OK 0
237#define NT 1 /* Not tested */
uwe4529d202007-08-23 13:34:59 +0000238
mkarcher74d30132010-07-22 18:04:15 +0000239/* Something happened that shouldn't happen, but we can go on */
240#define ERROR_NONFATAL 0x100
241
snelson9cba3c62010-01-07 20:09:33 +0000242/* cli_output.c */
hailfinger63932d42010-06-04 23:20:21 +0000243/* Let gcc and clang check for correct printf-style format strings. */
244int print(int type, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
hailfingere7326b22010-01-09 03:22:31 +0000245#define MSG_ERROR 0
246#define MSG_INFO 1
247#define MSG_DEBUG 2
248#define MSG_BARF 3
249#define msg_gerr(...) print(MSG_ERROR, __VA_ARGS__) /* general errors */
250#define msg_perr(...) print(MSG_ERROR, __VA_ARGS__) /* programmer errors */
251#define msg_cerr(...) print(MSG_ERROR, __VA_ARGS__) /* chip errors */
252#define msg_ginfo(...) print(MSG_INFO, __VA_ARGS__) /* general info */
253#define msg_pinfo(...) print(MSG_INFO, __VA_ARGS__) /* programmer info */
254#define msg_cinfo(...) print(MSG_INFO, __VA_ARGS__) /* chip info */
255#define msg_gdbg(...) print(MSG_DEBUG, __VA_ARGS__) /* general debug */
256#define msg_pdbg(...) print(MSG_DEBUG, __VA_ARGS__) /* programmer debug */
257#define msg_cdbg(...) print(MSG_DEBUG, __VA_ARGS__) /* chip debug */
258#define msg_gspew(...) print(MSG_BARF, __VA_ARGS__) /* general debug barf */
259#define msg_pspew(...) print(MSG_BARF, __VA_ARGS__) /* programmer debug barf */
260#define msg_cspew(...) print(MSG_BARF, __VA_ARGS__) /* chip debug barf */
snelson9cba3c62010-01-07 20:09:33 +0000261
hailfinger92cd8e32010-01-07 03:24:05 +0000262/* cli_classic.c */
263int cli_classic(int argc, char *argv[]);
David Hendricksf7924d12010-06-10 21:26:44 -0700264int cli_mfg(int argc, char *argv[]);
hailfinger92cd8e32010-01-07 03:24:05 +0000265
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +0800266/* cli_mfg.c */
267extern int set_ignore_fmap;
hailfinger80422e22009-12-13 22:28:00 +0000268
hailfinger40167462009-05-31 17:57:34 +0000269/* layout.c */
uwee06bcf82009-04-24 16:17:41 +0000270int read_romlayout(char *name);
stepan1037f6f2008-01-18 15:33:10 +0000271int find_romentry(char *name);
hailfinger4c47e9d2010-10-19 22:06:20 +0000272int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents);
David Hendricks6d62d752011-03-07 21:20:22 -0800273int add_fmap_entries(struct flashchip *flash);
Louis Yung-Chieh Lobb9049c2011-05-10 16:06:28 +0800274int get_num_include_args(void);
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800275int register_include_arg(char *name);
276int process_include_args(void);
David Hendricksc801adb2010-12-09 16:58:56 -0800277int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +0800278int handle_partial_read(
279 struct flashchip *flash,
280 uint8_t *buf,
281 int (*read) (struct flashchip *flash, uint8_t *buf, int start, int len));
282 /* RETURN: the number of partitions that have beenpartial read.
283 * ==0 means no partition is specified.
284 * < 0 means writing file error. */
uwe4529d202007-08-23 13:34:59 +0000285
stepan745615e2007-10-15 21:44:47 +0000286/* spi.c */
hailfinger68002c22009-07-10 21:08:55 +0000287struct spi_command {
288 unsigned int writecnt;
289 unsigned int readcnt;
290 const unsigned char *writearr;
291 unsigned char *readarr;
292};
hailfinger68002c22009-07-10 21:08:55 +0000293int spi_send_command(unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +0000294 const unsigned char *writearr, unsigned char *readarr);
hailfingerbb092112009-09-18 15:50:56 +0000295int spi_send_multicommand(struct spi_command *cmds);
hailfinger088dc812009-12-14 03:32:24 +0000296uint32_t spi_get_valid_read_addr(void);
uweaf9b4df2008-09-26 13:19:02 +0000297
ollie5b621572004-03-20 16:46:10 +0000298#endif /* !__FLASH_H__ */