Undo all PCI config space writes on shutdown. This means all chipset enables etc. will be undone on shutdown. Any writes which are one-shot should use the permanent ppci_write_* variants.
Extend the number of available register_shutdown slots to 32.
Signed-off-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Index: flashrom-pci_configspace_shutdown_restore/drkaiser.c =================================================================== --- flashrom-pci_configspace_shutdown_restore/drkaiser.c (Revision 1225) +++ flashrom-pci_configspace_shutdown_restore/drkaiser.c (Arbeitskopie) @@ -61,8 +61,7 @@
int drkaiser_shutdown(void) { - /* Write protect the flash again. */ - pci_write_word(pcidev_dev, PCI_MAGIC_DRKAISER_ADDR, 0); + /* Flash write is disabled automatically by PCI restore. */ pci_cleanup(pacc); release_io_perms(); return 0; Index: flashrom-pci_configspace_shutdown_restore/pcidev.c =================================================================== --- flashrom-pci_configspace_shutdown_restore/pcidev.c (Revision 1225) +++ flashrom-pci_configspace_shutdown_restore/pcidev.c (Arbeitskopie) @@ -21,6 +21,7 @@ #include <stdlib.h> #include <string.h> #include "flash.h" +#define NO_PCI_REDIRECT 1 #include "programmer.h"
uint32_t io_base_addr; @@ -90,7 +91,7 @@ uint32_t addr = 0, curaddr = 0;
pacc = pci_alloc(); /* Get the pci_access structure */ - pci_init(pacc); /* Initialize the PCI library */ + pci_init(pacc); /* Initialize the PCI library */ pci_scan_bus(pacc); /* We want to get the list of devices */ pci_filter_init(pacc, &filter);
@@ -142,3 +143,85 @@ (devs[i].status == NT) ? " (untested)" : ""); } } + +enum pci_write_type { + pci_write_type_byte, + pci_write_type_word, + pci_write_type_long, +}; + +struct undo_pci_write_data { + struct pci_dev dev; + int pos; + enum pci_write_type type; + union { + uint8_t bytedata; + uint16_t worddata; + uint32_t longdata; + }; +}; + +void undo_pci_write(void *p) +{ + struct undo_pci_write_data *data = p; + msg_pdbg("Restoring PCI config space for %02x:%02x:%01x pos 0x%02x\n", + data->dev.bus, data->dev.dev, data->dev.func, data->pos); + switch (data->type) { + case pci_write_type_byte: + pci_write_byte(&data->dev, data->pos, data->bytedata); + break; + case pci_write_type_word: + pci_write_word(&data->dev, data->pos, data->worddata); + break; + case pci_write_type_long: + pci_write_long(&data->dev, data->pos, data->longdata); + break; + } + /* p was allocated in register_undo_pci_write. */ + free(p); +} + +#define register_undo_pci_write(a, b, c) \ +{ \ + struct undo_pci_write_data *undo_pci_write_data; \ + undo_pci_write_data = malloc(sizeof(struct undo_pci_write_data)); \ + undo_pci_write_data->dev = *a; \ + undo_pci_write_data->pos = b; \ + undo_pci_write_data->type = pci_write_type_##c; \ + undo_pci_write_data->c##data = pci_read_##c(dev, pos); \ + register_shutdown(undo_pci_write, undo_pci_write_data); \ +} + +int rpci_write_byte(struct pci_dev *dev, int pos, uint8_t data) +{ + register_undo_pci_write(dev, pos, byte); + return pci_write_byte(dev, pos, data); +} + +int rpci_write_word(struct pci_dev *dev, int pos, uint16_t data) +{ + register_undo_pci_write(dev, pos, word); + return pci_write_word(dev, pos, data); +} + +int rpci_write_long(struct pci_dev *dev, int pos, uint32_t data) +{ + register_undo_pci_write(dev, pos, long); + return pci_write_long(dev, pos, data); +} + +int ppci_write_byte(struct pci_dev *dev, int pos, u8 data) +{ + return pci_write_byte(dev, pos, data); +} + +int ppci_write_word(struct pci_dev *dev, int pos, u16 data) +{ + return pci_write_word(dev, pos, data); +} + +int ppci_write_long(struct pci_dev *dev, int pos, u32 data) +{ + return pci_write_long(dev, pos, data); +} + Index: flashrom-pci_configspace_shutdown_restore/gfxnvidia.c =================================================================== --- flashrom-pci_configspace_shutdown_restore/gfxnvidia.c (Revision 1225) +++ flashrom-pci_configspace_shutdown_restore/gfxnvidia.c (Arbeitskopie) @@ -89,13 +89,9 @@
int gfxnvidia_shutdown(void) { - uint32_t reg32; - - /* Disallow access to flash interface (and re-enable screen). */ - reg32 = pci_read_long(pcidev_dev, 0x50); - reg32 |= (1 << 0); - pci_write_long(pcidev_dev, 0x50, reg32); - + /* Flash interface access is disabled (and screen enabled) automatically + * by PCI restore. + */ pci_cleanup(pacc); release_io_perms(); return 0; Index: flashrom-pci_configspace_shutdown_restore/atahpt.c =================================================================== --- flashrom-pci_configspace_shutdown_restore/atahpt.c (Revision 1225) +++ flashrom-pci_configspace_shutdown_restore/atahpt.c (Arbeitskopie) @@ -59,13 +59,7 @@
int atahpt_shutdown(void) { - uint32_t reg32; - - /* Disable flash access again. */ - reg32 = pci_read_long(pcidev_dev, REG_FLASH_ACCESS); - reg32 &= ~(1 << 24); - pci_write_long(pcidev_dev, REG_FLASH_ACCESS, reg32); - + /* Flash access is disabled automatically by PCI restore. */ pci_cleanup(pacc); release_io_perms(); return 0; Index: flashrom-pci_configspace_shutdown_restore/chipset_enable.c =================================================================== --- flashrom-pci_configspace_shutdown_restore/chipset_enable.c (Revision 1225) +++ flashrom-pci_configspace_shutdown_restore/chipset_enable.c (Arbeitskopie) @@ -498,17 +498,6 @@ return enable_flash_ich_dc_spi(dev, name, 10); }
-static void via_do_byte_merge(void * arg) -{ - struct pci_dev * dev = arg; - uint8_t val; - - msg_pdbg("Re-enabling byte merging\n"); - val = pci_read_byte(dev, 0x71); - val |= 0x40; - pci_write_byte(dev, 0x71, val); -} - static int via_no_byte_merge(struct pci_dev *dev, const char *name) { uint8_t val; @@ -519,7 +508,6 @@ msg_pdbg("Disabling byte merging\n"); val &= ~0x40; pci_write_byte(dev, 0x71, val); - register_shutdown(via_do_byte_merge, dev); } return NOT_DONE_YET; /* need to find south bridge, too */ } Index: flashrom-pci_configspace_shutdown_restore/flashrom.c =================================================================== --- flashrom-pci_configspace_shutdown_restore/flashrom.c (Revision 1225) +++ flashrom-pci_configspace_shutdown_restore/flashrom.c (Arbeitskopie) @@ -441,7 +441,7 @@ {}, /* This entry corresponds to PROGRAMMER_INVALID. */ };
-#define SHUTDOWN_MAXFN 4 +#define SHUTDOWN_MAXFN 32 static int shutdown_fn_count = 0; struct shutdown_func_data { void (*func) (void *data); Index: flashrom-pci_configspace_shutdown_restore/programmer.h =================================================================== --- flashrom-pci_configspace_shutdown_restore/programmer.h (Revision 1225) +++ flashrom-pci_configspace_shutdown_restore/programmer.h (Arbeitskopie) @@ -212,7 +212,25 @@ }; uint32_t pcidev_validate(struct pci_dev *dev, uint32_t bar, const struct pcidev_status *devs); uint32_t pcidev_init(uint16_t vendor_id, uint32_t bar, const struct pcidev_status *devs); +/* rpci_write_* are reversible writes. The original PCI config space register + * contents will be restored on shutdown. + */ +int rpci_write_byte(struct pci_dev *dev, int pos, u8 data); +int rpci_write_word(struct pci_dev *dev, int pos, u16 data); +int rpci_write_long(struct pci_dev *dev, int pos, u32 data); +/* ppci_write_* are permanent writes, an alias for pci_write_*. */ +int ppci_write_byte(struct pci_dev *dev, int pos, u8 data); +int ppci_write_word(struct pci_dev *dev, int pos, u16 data); +int ppci_write_long(struct pci_dev *dev, int pos, u32 data); + +#if NO_PCI_REDIRECT +/* Don't touch pci_write_* definitions. */ +#else +#define pci_write_byte rpci_write_byte +#define pci_write_word rpci_write_word +#define pci_write_long rpci_write_long #endif +#endif
/* print.c */ #if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_NICINTEL_SPI >= 1