blob: fb19c001b504104f4ce9b8267d7f95a550f107db [file] [log] [blame]
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2000 Ronald G. Minnich <rminnich@gmail.com>
6 * Copyright (C) 2005-2009 coresystems GmbH
7 * Copyright (C) 2006-2009 Carl-Daniel Hailfinger
8 *
9 * 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.
13 *
14 * 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.
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000018 */
19
20#ifndef __PROGRAMMER_H__
21#define __PROGRAMMER_H__ 1
22
Nico Huber89569d62023-01-12 23:31:40 +010023#include <limits.h>
Felix Singerb8db74a2022-08-19 00:19:26 +020024#include <stdbool.h>
Nico Huber1cf407b2017-11-10 20:18:23 +010025#include <stdint.h>
Nico Huber019810f2023-01-29 17:11:24 +000026#include <stdbool.h>
Nico Huber1cf407b2017-11-10 20:18:23 +010027
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000028#include "flash.h" /* for chipaddr and flashctx */
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000029
Stefan Tauneraf358d62012-12-27 18:40:26 +000030enum programmer_type {
31 PCI = 1, /* to detect uninitialized values */
32 USB,
33 OTHER,
34};
35
Stefan Tauner4b24a2d2012-12-27 18:40:36 +000036struct dev_entry {
37 uint16_t vendor_id;
38 uint16_t device_id;
39 const enum test_state status;
40 const char *vendor_name;
41 const char *device_name;
42};
43
Nico Hubere3a26882023-01-11 21:45:51 +010044struct flashprog_programmer;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000045struct programmer_entry {
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000046 const char *name;
Stefan Tauneraf358d62012-12-27 18:40:26 +000047 const enum programmer_type type;
48 union {
Stefan Tauner4b24a2d2012-12-27 18:40:36 +000049 const struct dev_entry *const dev;
Stefan Tauneraf358d62012-12-27 18:40:26 +000050 const char *const note;
51 } devs;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000052
Nico Hubere3a26882023-01-11 21:45:51 +010053 int (*init) (struct flashprog_programmer *);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000054
Stefan Taunerf80419c2014-05-02 15:41:42 +000055 void (*delay) (unsigned int usecs);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000056};
57
Thomas Heijligen633d6db2021-03-31 19:09:44 +020058extern const struct programmer_entry *const programmer_table[];
Thomas Heijligend0fcce22021-05-19 13:53:34 +020059extern const size_t programmer_table_size;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000060
Thomas Heijligen40d32332021-06-10 15:17:53 +020061/* programmer drivers */
Thomas Heijligen40d32332021-06-10 15:17:53 +020062extern const struct programmer_entry programmer_atahpt;
Thomas Heijligen40d32332021-06-10 15:17:53 +020063extern const struct programmer_entry programmer_atapromise;
Thomas Heijligen1535db42021-06-14 13:20:09 +020064extern const struct programmer_entry programmer_atavia;
Thomas Heijligen40d32332021-06-10 15:17:53 +020065extern const struct programmer_entry programmer_buspirate_spi;
Thomas Heijligen1535db42021-06-14 13:20:09 +020066extern const struct programmer_entry programmer_ch341a_spi;
Nicholas Chin197b7c72022-10-23 13:10:31 -060067extern const struct programmer_entry programmer_ch347_spi;
Thomas Heijligen40d32332021-06-10 15:17:53 +020068extern const struct programmer_entry programmer_dediprog;
69extern const struct programmer_entry programmer_developerbox;
Thomas Heijligen40d32332021-06-10 15:17:53 +020070extern const struct programmer_entry programmer_digilent_spi;
Thomas Heijligen1535db42021-06-14 13:20:09 +020071extern const struct programmer_entry programmer_drkaiser;
72extern const struct programmer_entry programmer_dummy;
73extern const struct programmer_entry programmer_ft2232_spi;
74extern const struct programmer_entry programmer_gfxnvidia;
75extern const struct programmer_entry programmer_internal;
76extern const struct programmer_entry programmer_it8212;
Thomas Heijligen40d32332021-06-10 15:17:53 +020077extern const struct programmer_entry programmer_jlink_spi;
Steve Markgraf61899472023-01-09 23:06:52 +010078extern const struct programmer_entry programmer_linux_gpio_spi;
Thomas Heijligen1535db42021-06-14 13:20:09 +020079extern const struct programmer_entry programmer_linux_mtd;
80extern const struct programmer_entry programmer_linux_spi;
81extern const struct programmer_entry programmer_mstarddc_spi;
Thomas Heijligen40d32332021-06-10 15:17:53 +020082extern const struct programmer_entry programmer_ni845x_spi;
Thomas Heijligen1535db42021-06-14 13:20:09 +020083extern const struct programmer_entry programmer_nic3com;
84extern const struct programmer_entry programmer_nicintel;
85extern const struct programmer_entry programmer_nicintel_eeprom;
86extern const struct programmer_entry programmer_nicintel_spi;
87extern const struct programmer_entry programmer_nicnatsemi;
88extern const struct programmer_entry programmer_nicrealtek;
89extern const struct programmer_entry programmer_ogp_spi;
90extern const struct programmer_entry programmer_pickit2_spi;
91extern const struct programmer_entry programmer_pony_spi;
92extern const struct programmer_entry programmer_rayer_spi;
93extern const struct programmer_entry programmer_satamv;
94extern const struct programmer_entry programmer_satasii;
Thomas Heijligen40d32332021-06-10 15:17:53 +020095extern const struct programmer_entry programmer_serprog;
Thomas Heijligen1535db42021-06-14 13:20:09 +020096extern const struct programmer_entry programmer_stlinkv3_spi;
97extern const struct programmer_entry programmer_usbblaster_spi;
Jean THOMASe28d8e42022-10-11 17:54:30 +020098extern const struct programmer_entry programmer_dirtyjtag_spi;
Thomas Heijligen40d32332021-06-10 15:17:53 +020099
Nico Huber2b66ad92023-01-11 20:15:15 +0100100struct flashprog_programmer {
101 const struct programmer_entry *driver;
102 char *param; /* TODO: Replace with flashprog_cfg (cf. flashrom/master) */
Nico Huber89569d62023-01-12 23:31:40 +0100103 void *data;
Nico Huber2b66ad92023-01-11 20:15:15 +0100104};
105
106int programmer_init(struct flashprog_programmer *);
107int programmer_shutdown(struct flashprog_programmer *);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000108
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000109struct bitbang_spi_master {
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000110 /* Note that CS# is active low, so val=0 means the chip is active. */
Anastasia Klimchuk0e788182021-05-26 09:54:08 +1000111 void (*set_cs) (int val, void *spi_data);
112 void (*set_sck) (int val, void *spi_data);
113 void (*set_mosi) (int val, void *spi_data);
114 int (*get_miso) (void *spi_data);
115 void (*request_bus) (void *spi_data);
116 void (*release_bus) (void *spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +0100117 /* optional functions to optimize xfers */
Anastasia Klimchuk0e788182021-05-26 09:54:08 +1000118 void (*set_sck_set_mosi) (int sck, int mosi, void *spi_data);
119 int (*set_sck_get_miso) (int sck, void *spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000120 /* Length of half a clock period in usecs. */
121 unsigned int half_period;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000122};
123
Patrick Georgi32508eb2012-07-20 20:35:14 +0000124struct pci_dev;
Edward O'Callaghan15004ba2021-11-13 13:14:06 +1100125struct pci_filter;
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +0000126
127/* pcidev.c */
Stefan Tauner0ccec8f2014-06-01 23:49:03 +0000128// FIXME: This needs to be local, not global(?)
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +0000129extern struct pci_access *pacc;
130int pci_init_common(void);
131uintptr_t pcidev_readbar(struct pci_dev *dev, int bar);
132struct pci_dev *pcidev_init(const struct dev_entry *devs, int bar);
Edward O'Callaghan15004ba2021-11-13 13:14:06 +1100133struct pci_dev *pcidev_scandev(struct pci_filter *filter, struct pci_dev *start);
Edward O'Callaghan48a94662022-02-26 11:36:17 +1100134struct pci_dev *pcidev_find_vendorclass(uint16_t vendor, uint16_t devclass);
Edward O'Callaghan6c73e272021-11-13 17:56:20 +1100135struct pci_dev *pcidev_card_find(uint16_t vendor, uint16_t device, uint16_t card_vendor, uint16_t card_device);
Edward O'Callaghan19ce50d2021-11-13 17:59:18 +1100136struct pci_dev *pcidev_find(uint16_t vendor, uint16_t device);
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +0000137/* rpci_write_* are reversible writes. The original PCI config space register
138 * contents will be restored on shutdown.
Youness Alaouia54ceb12017-07-26 18:03:36 -0400139 * To clone the pci_dev instances internally, the `pacc` global
140 * variable has to reference a pci_access method that is compatible
141 * with the given pci_dev handle. The referenced pci_access (not
142 * the variable) has to stay valid until the shutdown handlers are
143 * finished.
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +0000144 */
145int rpci_write_byte(struct pci_dev *dev, int reg, uint8_t data);
146int rpci_write_word(struct pci_dev *dev, int reg, uint16_t data);
147int rpci_write_long(struct pci_dev *dev, int reg, uint32_t data);
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +0000148
149#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000150struct penable {
151 uint16_t vendor_id;
152 uint16_t device_id;
Nico Huber019810f2023-01-29 17:11:24 +0000153 bool match_revision;
154 uint8_t revision_id;
Nico Huber2e50cdc2018-09-23 20:20:26 +0200155 enum chipbustype buses;
Stefan Tauner2c20b282012-07-28 19:35:26 +0000156 const enum test_state status;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000157 const char *vendor_name;
158 const char *device_name;
Nico Huber929d2e12023-01-12 00:47:05 +0100159 int (*doit) (struct flashprog_programmer *, struct pci_dev *, const char *name);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000160};
161
162extern const struct penable chipset_enables[];
163
Carl-Daniel Hailfinger580d29a2011-05-05 07:12:40 +0000164enum board_match_phase {
165 P1,
166 P2,
167 P3
168};
169
Carl-Daniel Hailfinger97d5b122011-08-31 16:19:50 +0000170struct board_match {
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000171 /* Any device, but make it sensible, like the ISA bridge. */
172 uint16_t first_vendor;
173 uint16_t first_device;
174 uint16_t first_card_vendor;
175 uint16_t first_card_device;
176
177 /* Any device, but make it sensible, like
178 * the host bridge. May be NULL.
179 */
180 uint16_t second_vendor;
181 uint16_t second_device;
182 uint16_t second_card_vendor;
183 uint16_t second_card_device;
184
Stefan Tauner7bcacb12011-05-26 01:35:19 +0000185 /* Pattern to match DMI entries. May be NULL. */
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000186 const char *dmi_pattern;
187
Stefan Tauner7bcacb12011-05-26 01:35:19 +0000188 /* The vendor / part name from the coreboot table. May be NULL. */
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000189 const char *lb_vendor;
190 const char *lb_part;
191
Carl-Daniel Hailfinger580d29a2011-05-05 07:12:40 +0000192 enum board_match_phase phase;
193
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000194 const char *vendor_name;
195 const char *board_name;
196
197 int max_rom_decode_parallel;
Stefan Tauner2c20b282012-07-28 19:35:26 +0000198 const enum test_state status;
Nico Huber7c717c32023-01-12 00:28:15 +0100199 int (*enable) (struct flashprog_programmer *); /* May be NULL. */
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000200};
201
Carl-Daniel Hailfinger97d5b122011-08-31 16:19:50 +0000202extern const struct board_match board_matches[];
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000203
204struct board_info {
205 const char *vendor;
206 const char *name;
Stefan Tauner2c20b282012-07-28 19:35:26 +0000207 const enum test_state working;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000208#ifdef CONFIG_PRINT_WIKI
209 const char *url;
210 const char *note;
211#endif
212};
213
214extern const struct board_info boards_known[];
215extern const struct board_info laptops_known[];
216#endif
217
218/* udelay.c */
Stefan Taunerf80419c2014-05-02 15:41:42 +0000219void myusec_delay(unsigned int usecs);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000220void myusec_calibrate_delay(void);
Stefan Taunerf80419c2014-05-02 15:41:42 +0000221void internal_sleep(unsigned int usecs);
222void internal_delay(unsigned int usecs);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000223
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +0000224#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000225/* board_enable.c */
Stefan Tauner600576b2014-06-12 22:57:36 +0000226int selfcheck_board_enables(void);
Jacob Garber1c091d12019-08-12 11:14:14 -0600227int board_parse_parameter(const char *boardstring, char **vendor, char **model);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000228void w836xx_ext_enter(uint16_t port);
229void w836xx_ext_leave(uint16_t port);
Carl-Daniel Hailfingerf5e62cb2012-05-06 22:48:01 +0000230void probe_superio_winbond(void);
Nico Huber89569d62023-01-12 23:31:40 +0100231int it8705f_write_enable(struct flashprog_programmer *, uint8_t port);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000232uint8_t sio_read(uint16_t port, uint8_t reg);
233void sio_write(uint16_t port, uint8_t reg, uint8_t data);
234void sio_mask(uint16_t port, uint8_t reg, uint8_t data, uint8_t mask);
Nico Huber7c717c32023-01-12 00:28:15 +0100235void board_handle_before_superio(struct flashprog_programmer *);
236void board_handle_before_laptop(struct flashprog_programmer *);
237int board_flash_enable(struct flashprog_programmer *, const char *vendor, const char *model, const char *cb_vendor, const char *cb_model);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000238
239/* chipset_enable.c */
Nico Huber929d2e12023-01-12 00:47:05 +0100240int chipset_flash_enable(struct flashprog_programmer *);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000241
242/* processor_enable.c */
243int processor_flash_enable(void);
Carl-Daniel Hailfinger580d29a2011-05-05 07:12:40 +0000244#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000245
Thomas Heijligenb3287b42021-12-14 17:25:49 +0100246#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000247/* cbtable.c */
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000248int cb_parse_table(const char **vendor, const char **model);
Nico Huber519be662018-12-23 20:03:35 +0100249int cb_check_image(const uint8_t *bios, unsigned int size);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000250
251/* dmi.c */
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000252#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000253extern int has_dmi_support;
254void dmi_init(void);
255int dmi_match(const char *pattern);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000256#endif // defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000257
258/* internal.c */
Nico Huber89569d62023-01-12 23:31:40 +0100259struct internal_data {
260 size_t max_rom_decode;
261};
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000262struct superio {
263 uint16_t vendor;
264 uint16_t port;
265 uint16_t model;
266};
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000267extern struct superio superios[];
268extern int superio_count;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000269#define SUPERIO_VENDOR_NONE 0x0
270#define SUPERIO_VENDOR_ITE 0x1
Carl-Daniel Hailfingerf5e62cb2012-05-06 22:48:01 +0000271#define SUPERIO_VENDOR_WINBOND 0x2
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +0000272#endif
Edward O'Callaghan19ce50d2021-11-13 17:59:18 +1100273
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000274#if CONFIG_INTERNAL == 1
275extern int is_laptop;
Felix Singerd1ab7d22022-08-19 03:03:47 +0200276extern bool laptop_ok;
Felix Singerb8db74a2022-08-19 00:19:26 +0200277extern bool force_boardenable;
278extern bool force_boardmismatch;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000279void probe_superio(void);
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000280int register_superio(struct superio s);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000281extern enum chipbustype internal_buses_supported;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000282#endif
283
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000284/* bitbang_spi.c */
Anastasia Klimchuka447c122021-05-31 11:20:01 +1000285int register_spi_bitbang_master(const struct bitbang_spi_master *master, void *spi_data);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000286
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100287
Nico Huberc3b02dc2023-08-12 01:13:45 +0200288/* flashprog.c */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000289// FIXME: These need to be local, not global
Felix Singer980d6b82022-08-19 02:48:15 +0200290extern bool programmer_may_write;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000291extern unsigned long flashbase;
Stefan Tauner66652442011-06-26 17:38:17 +0000292char *extract_programmer_param(const char *param_name);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000293
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000294/* spi.c */
Michael Karcher62797512011-05-11 17:07:02 +0000295#define MAX_DATA_UNSPECIFIED 0
296#define MAX_DATA_READ_UNLIMITED 64 * 1024
297#define MAX_DATA_WRITE_UNLIMITED 256
Nico Huber1cf407b2017-11-10 20:18:23 +0100298
299#define SPI_MASTER_4BA (1U << 0) /**< Can handle 4-byte addresses */
Nico Huberdc5af542018-12-22 16:54:59 +0100300#define SPI_MASTER_NO_4BA_MODES (1U << 1) /**< Compatibility modes (i.e. extended address
301 register, 4BA mode switch) don't work */
Nico Huberd5185632024-01-05 18:44:41 +0100302#define SPI_MASTER_DUAL_IN (1U << 2) /**< Can read two bits at once (bidirectional
303 MOSI and MISO) */
304#define SPI_MASTER_DUAL_IO (1U << 3) /**< Can transfer two bits at once (bidirectional
305 MOSI and MISO) */
306#define SPI_MASTER_QUAD_IN (1U << 4) /**< Can read four bits at once (bidirectional
307 MOSI and MISO + IO2 + IO3) */
308#define SPI_MASTER_QUAD_IO (1U << 5) /**< Can transfer four bits at once (bidirectional
309 MOSI and MISO + IO2 + IO3) */
310#define SPI_MASTER_QPI (1U << 6) /**< Can send commands with quad i/o */
311#define SPI_MASTER_DTR_IN (1U << 7) /**< Double Transfer Rate: Can read two bits
312 per clock cycle per line */
Nico Huber1cf407b2017-11-10 20:18:23 +0100313
Nico Huberd5185632024-01-05 18:44:41 +0100314/* Shorthands: */
315#define SPI_MASTER_DUAL (SPI_MASTER_DUAL_IN | SPI_MASTER_DUAL_IO)
316#define SPI_MASTER_QUAD (SPI_MASTER_QUAD_IN | SPI_MASTER_QUAD_IO)
317
318struct spi_command;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000319struct spi_master {
Nico Huber1cf407b2017-11-10 20:18:23 +0100320 uint32_t features;
Stefan Tauner23e10b82016-01-23 16:16:49 +0000321 unsigned int max_data_read; // (Ideally,) maximum data read size in one go (excluding opcode+address).
322 unsigned int max_data_write; // (Ideally,) maximum data write size in one go (excluding opcode+address).
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000323 int (*command)(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000324 const unsigned char *writearr, unsigned char *readarr);
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000325 int (*multicommand)(const struct flashctx *flash, struct spi_command *cmds);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000326
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000327 /* Optimized functions for this master */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000328 int (*read)(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000329 int (*write_256)(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
330 int (*write_aai)(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
Anastasia Klimchuk7783f2f2021-07-05 09:18:06 +1000331 int (*shutdown)(void *data);
Nikolai Artemieve7a41e32022-11-28 17:40:56 +1100332 bool (*probe_opcode)(const struct flashctx *flash, uint8_t opcode);
Edward O'Callaghan13f90e62021-01-06 14:10:52 +1100333 void *data;
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000334};
335
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000336int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000337 const unsigned char *writearr, unsigned char *readarr);
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000338int default_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000339int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000340int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
341int default_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
Nikolai Artemieve7a41e32022-11-28 17:40:56 +1100342bool default_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode);
Nico Huber89569d62023-01-12 23:31:40 +0100343int register_spi_master(const struct spi_master *mst, size_t max_rom_decode, void *data);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000344
Kyösti Mälkki78cd0872013-09-14 23:36:57 +0000345/* The following enum is needed by ich_descriptor_tool and ich* code as well as in chipset_enable.c. */
Stefan Taunera8d838d2011-11-06 23:51:09 +0000346enum ich_chipset {
347 CHIPSET_ICH_UNKNOWN,
Stefan Tauner92d6a862013-10-25 00:33:37 +0000348 CHIPSET_ICH,
349 CHIPSET_ICH2345,
Kyösti Mälkki78cd0872013-09-14 23:36:57 +0000350 CHIPSET_ICH6,
Stefan Tauner92d6a862013-10-25 00:33:37 +0000351 CHIPSET_POULSBO, /* SCH U* */
352 CHIPSET_TUNNEL_CREEK, /* Atom E6xx */
353 CHIPSET_CENTERTON, /* Atom S1220 S1240 S1260 */
Kyösti Mälkki78cd0872013-09-14 23:36:57 +0000354 CHIPSET_ICH7,
Stefan Taunera8d838d2011-11-06 23:51:09 +0000355 CHIPSET_ICH8,
356 CHIPSET_ICH9,
357 CHIPSET_ICH10,
358 CHIPSET_5_SERIES_IBEX_PEAK,
359 CHIPSET_6_SERIES_COUGAR_POINT,
Stefan Tauner2abab942012-04-27 20:41:23 +0000360 CHIPSET_7_SERIES_PANTHER_POINT,
Duncan Laurie90eb2262013-03-15 03:12:29 +0000361 CHIPSET_8_SERIES_LYNX_POINT,
Duncan Laurie4095ed72014-08-20 15:39:32 +0000362 CHIPSET_BAYTRAIL, /* Actually all with Silvermont architecture: Bay Trail, Avoton/Rangeley */
Duncan Laurie90eb2262013-03-15 03:12:29 +0000363 CHIPSET_8_SERIES_LYNX_POINT_LP,
364 CHIPSET_8_SERIES_WELLSBURG,
Duncan Laurie823096e2014-08-20 15:39:38 +0000365 CHIPSET_9_SERIES_WILDCAT_POINT,
Nico Huber51205912017-03-17 17:59:54 +0100366 CHIPSET_9_SERIES_WILDCAT_POINT_LP,
Nico Huber93c30692017-03-20 14:25:09 +0100367 CHIPSET_100_SERIES_SUNRISE_POINT, /* also 6th/7th gen Core i/o (LP) variants */
David Hendricksa5216362017-08-08 20:02:22 -0700368 CHIPSET_C620_SERIES_LEWISBURG,
Thomas Heijligen5ec84b32019-03-19 17:00:03 +0100369 CHIPSET_300_SERIES_CANNON_POINT,
Michał Żygowski5c9f5422021-06-16 15:13:54 +0200370 CHIPSET_500_SERIES_TIGER_POINT,
Nico Huber37509862019-01-18 14:23:02 +0100371 CHIPSET_APOLLO_LAKE,
Angel Pons4db0fdf2020-07-10 17:04:10 +0200372 CHIPSET_GEMINI_LAKE,
Werner Zehe57d4e42022-01-03 09:44:29 +0100373 CHIPSET_ELKHART_LAKE,
Stefan Taunera8d838d2011-11-06 23:51:09 +0000374};
375
Stefan Tauner2abab942012-04-27 20:41:23 +0000376/* ichspi.c */
377#if CONFIG_INTERNAL == 1
Nico Huber560111e2017-04-26 12:27:17 +0200378int ich_init_spi(void *spibar, enum ich_chipset ich_generation);
379int via_init_spi(uint32_t mmio_base);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000380
Stefan Taunerdbac46c2013-08-13 22:10:41 +0000381/* amd_imc.c */
Nico Huber4d51e072023-01-29 17:56:29 +0000382int handle_imc(struct pci_dev *);
Rudolf Marek70e14592013-07-25 22:58:56 +0000383
Nico Huber735b1862023-01-29 18:28:45 +0000384/* amd_spi100.c */
Nico Hubere3c305d2023-01-29 21:45:56 +0000385int amd_spi100_probe(void *const spibar, void *const memory_mapping, const size_t mapped_len);
Nico Huber735b1862023-01-29 18:28:45 +0000386
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000387/* it87spi.c */
388void enter_conf_mode_ite(uint16_t port);
389void exit_conf_mode_ite(uint16_t port);
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000390void probe_superio_ite(void);
Nico Huber89569d62023-01-12 23:31:40 +0100391int init_superio_ite(struct flashprog_programmer *);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000392
Nico Huber2f753792023-03-28 00:46:50 +0200393#if CONFIG_LINUX_MTD == 1 && LINUX_MTD_AS_INTERNAL == 1
David Hendricksf9a30552015-05-23 20:30:30 -0700394/* trivial wrapper to avoid cluttering internal_init() with #if */
Nico Hubere3a26882023-01-11 21:45:51 +0100395static inline int try_mtd(struct flashprog_programmer *prog) { return programmer_linux_mtd.init(prog); };
David Hendricksf9a30552015-05-23 20:30:30 -0700396#else
Nico Hubere3a26882023-01-11 21:45:51 +0100397static inline int try_mtd(struct flashprog_programmer *prog) { return 1; };
David Hendricksf9a30552015-05-23 20:30:30 -0700398#endif
399
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +0000400/* mcp6x_spi.c */
401int mcp6x_spi_init(int want_spi);
402
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000403/* sb600spi.c */
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000404int sb600_probe_spi(struct pci_dev *dev);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000405
406/* wbsio_spi.c */
Nico Huber7c717c32023-01-12 00:28:15 +0100407int wbsio_check_for_spi(struct flashprog_programmer *);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000408#endif
409
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +0000410/* opaque.c */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000411struct opaque_master {
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +0000412 int max_data_read;
413 int max_data_write;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000414 /* Specific functions for this master */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000415 int (*probe) (struct flashctx *flash);
416 int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000417 int (*write) (struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000418 int (*erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen);
Anastasia Klimchuka1fed9f2021-08-03 14:08:02 +1000419 int (*shutdown)(void *data);
Edward O'Callaghan13f90e62021-01-06 14:10:52 +1100420 void *data;
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +0000421};
Anastasia Klimchuk21b20212021-05-13 12:28:47 +1000422int register_opaque_master(const struct opaque_master *mst, void *data);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +0000423
Edward O'Callaghan63f6a372022-08-12 12:56:43 +1000424/* parallel.c */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000425struct par_master {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000426 void (*chip_writeb) (const struct flashctx *flash, uint8_t val, chipaddr addr);
427 void (*chip_writew) (const struct flashctx *flash, uint16_t val, chipaddr addr);
428 void (*chip_writel) (const struct flashctx *flash, uint32_t val, chipaddr addr);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000429 void (*chip_writen) (const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000430 uint8_t (*chip_readb) (const struct flashctx *flash, const chipaddr addr);
431 uint16_t (*chip_readw) (const struct flashctx *flash, const chipaddr addr);
432 uint32_t (*chip_readl) (const struct flashctx *flash, const chipaddr addr);
433 void (*chip_readn) (const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Nico Huber0e76d992023-01-12 20:22:55 +0100434
435 void *(*map_flash) (const char *descr, uintptr_t phys_addr, size_t len);
436 void (*unmap_flash) (void *virt_addr, size_t len);
437
Anastasia Klimchuke6953e52021-08-26 10:10:32 +1000438 int (*shutdown)(void *data);
Edward O'Callaghan13f90e62021-01-06 14:10:52 +1100439 void *data;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000440};
Nico Huber89569d62023-01-12 23:31:40 +0100441int register_par_master(const struct par_master *mst, const enum chipbustype buses, size_t max_rom_decode, void *data);
Edward O'Callaghan63f6a372022-08-12 12:56:43 +1000442
443/* programmer.c */
444void *fallback_map(const char *descr, uintptr_t phys_addr, size_t len);
445void fallback_unmap(void *virt_addr, size_t len);
446void fallback_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
447void fallback_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
448void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
449uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr);
450uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr);
451void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Nico Huber89569d62023-01-12 23:31:40 +0100452#define DEFAULT_MAX_DECODE_PARALLEL (16*MiB)
453#define MAX_ROM_DECODE_UNLIMITED UINT32_MAX
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000454struct registered_master {
Nico Huber89569d62023-01-12 23:31:40 +0100455 size_t max_rom_decode;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000456 enum chipbustype buses_supported;
457 union {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000458 struct par_master par;
459 struct spi_master spi;
460 struct opaque_master opaque;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000461 };
462};
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000463extern struct registered_master registered_masters[];
464extern int registered_master_count;
Stefan Tauner5c316f92015-02-08 21:57:52 +0000465int register_master(const struct registered_master *mst);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000466
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000467
468/* serial.c */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000469#if IS_WINDOWS
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000470typedef HANDLE fdtype;
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000471#define SER_INV_FD INVALID_HANDLE_VALUE
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000472#else
473typedef int fdtype;
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000474#define SER_INV_FD -1
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000475#endif
476
477void sp_flush_incoming(void);
Stefan Tauner72587f82016-01-04 03:05:15 +0000478fdtype sp_openserport(char *dev, int baud);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000479extern fdtype sp_fd;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600480int serialport_config(fdtype fd, int baud);
David Hendricks8bb20212011-06-14 01:35:36 +0000481int serialport_shutdown(void *data);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000482int serialport_write(const unsigned char *buf, unsigned int writecnt);
483int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000484int serialport_read(unsigned char *buf, unsigned int readcnt);
Stefan Tauner00e16082013-04-01 00:45:38 +0000485int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read);
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000486
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000487/* Serial port/pin mapping:
488
489 1 CD <-
490 2 RXD <-
491 3 TXD ->
492 4 DTR ->
493 5 GND --
494 6 DSR <-
495 7 RTS ->
496 8 CTS <-
497 9 RI <-
498*/
499enum SP_PIN {
500 PIN_CD = 1,
501 PIN_RXD,
502 PIN_TXD,
503 PIN_DTR,
504 PIN_GND,
505 PIN_DSR,
506 PIN_RTS,
507 PIN_CTS,
508 PIN_RI,
509};
510
511void sp_set_pin(enum SP_PIN pin, int val);
512int sp_get_pin(enum SP_PIN pin);
513
Nico Huber1cf407b2017-11-10 20:18:23 +0100514/* spi_master feature checks */
515static inline bool spi_master_4ba(const struct flashctx *const flash)
516{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100517 return flash->mst.spi->features & SPI_MASTER_4BA;
Nico Huber1cf407b2017-11-10 20:18:23 +0100518}
Nico Huberdc5af542018-12-22 16:54:59 +0100519static inline bool spi_master_no_4ba_modes(const struct flashctx *const flash)
520{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100521 return flash->mst.spi->features & SPI_MASTER_NO_4BA_MODES;
Nico Huberdc5af542018-12-22 16:54:59 +0100522}
Nico Huber1cf407b2017-11-10 20:18:23 +0100523
Daniel Thompson1d507a02018-07-12 11:02:28 +0100524/* usbdev.c */
525struct libusb_device_handle;
526struct libusb_context;
527struct libusb_device_handle *usb_dev_get_by_vid_pid_serial(
528 struct libusb_context *usb_ctx, uint16_t vid, uint16_t pid, const char *serialno);
529struct libusb_device_handle *usb_dev_get_by_vid_pid_number(
530 struct libusb_context *usb_ctx, uint16_t vid, uint16_t pid, unsigned int num);
531
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +0000532#endif /* !__PROGRAMMER_H__ */