Add a bunch of new/tested stuff and various small changes 16

Tested Mainboards:
OK:
 - Acer V75-M (used in IBM Aptiva 2170-G
   http://www.flashrom.org/pipermail/flashrom/2012-December/010300.html
 - ASRock 4CoreDual-VSTA with W39V040FB
   http://paste.flashrom.org/view.php?id=1446
 - ASRock 775Dual-VSTA
   http://www.flashrom.org/pipermail/flashrom/2012-December/010294.html
 - ASRock E350M1/USB3
   http://paste.flashrom.org/view.php?id=1465
 - ASUS P5B-VM
   http://www.flashrom.org/pipermail/flashrom/2012-December/010351.html
 - ASUS SABERTOOTH 990FX R2.0
   http://www.flashrom.org/pipermail/flashrom/2012-December/010210.html
 - Elitegroup A928 (including a laptop whitelist board enable)
   http://www.flashrom.org/pipermail/flashrom/2012-November/010119.html
 - EVGA 122-CK-NF68
   Reported by Stephanie Daugherty on IRC
   http://paste.flashrom.org/view.php?id=1431
 - GIGABYTE GA-A75M-UD2H
   Reported by Soul_keeper on IRC
   http://paste.flashrom.org/view.php?id=1490
 - Intel D945GCNL
   Add board enable to override laptop detection too.
   http://www.flashrom.org/pipermail/flashrom/2012-December/010276.html
 - MSI G33M (MS-7357)
   http://www.flashrom.org/pipermail/flashrom/2012-October/010056.html
 - Shuttle FB61
   http://www.flashrom.org/pipermail/flashrom/2012-November/010105.html
 - Tyan S4882 (Thunder K8QS Pro)
   Reported on IRC
NOT OK:
   Alienware Aurora-R2
   http://www.flashrom.org/pipermail/flashrom/2012-December/010225.html
   Biostar H61MU3
   http://www.flashrom.org/pipermail/flashrom/2012-November/010144.html
   Dell OptiPlex 7010
   http://paste.flashrom.org/view.php?id=1481
   Intel DH67CL
   http://www.flashrom.org/pipermail/flashrom/2012-November/010112.html
   Supermicro X9DRT-HF+
   http://www.flashrom.org/pipermail/flashrom/2012-November/010155.html
   Supermicro X9DRW
   http://www.flashrom.org/pipermail/flashrom/2012-November/010150.html

Tested flash chips:
 - Atmel AT25FS010 to PREW (+PREW)
   http://paste.flashrom.org/view.php?id=1484
 - Eon EN25F64 to PREW (+EW)
   http://www.flashrom.org/pipermail/flashrom/2012-December/010210.html
 - Spansion S25FL032A/P to PREW (+EW)
   http://paste.flashrom.org/view.php?id=1510
 - ST M29F002T/NT to PREW (+PREW)
   http://www.flashrom.org/pipermail/flashrom/2012-December/010300.html
 - Winbond W25X10 to PREW (+PREW)
   http://paste.flashrom.org/view.php?id=1486

Tested chipsets:
 - NVIDIA MCP78S http://www.flashrom.org/pipermail/flashrom/2012-November/010176.html
 - SiS 650 http://www.flashrom.org/pipermail/flashrom/2012-November/010119.html

Miscellaneous:
- Typo in GA-X58A-UDR3 (correct is GA-X58A-UD3R).
- Force 2-digit hex numbers in prints were it makes sense.
- Share code between enable_flash_sis530() and enable_flash_sis540().
- Some SST 25 series chips support both WRSR enable commands...
- S25FL032A and S25FL064A share the IDs with their P versions, so rename them.
- Fix a few memleaks in serprog.
- Dediprog uses UINT_MAX so include limits.h (fixes the Windows build of dediprog)
- Add (another) hint regarding the mandatory -p parameter to the manpage
  to make Debian bug #690478 happy.
  http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=690478
- Fix whitespace issues.
- On shutdown, reset count of registered programmers (by Nico Huber)
- Fix atahpt.c shutdown.
  The order of pcidev_init, register_shutdown and rpci_write_* is important!
  Thanks to Roy for reporting the problem and testing the fix.

Corresponding to flashrom svn r1640.

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Acked-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
diff --git a/chipset_enable.c b/chipset_enable.c
index 267008c..0dc1d7e 100644
--- a/chipset_enable.c
+++ b/chipset_enable.c
@@ -95,19 +95,20 @@
 
 static int enable_flash_sis_mapping(struct pci_dev *dev, const char *name)
 {
+	#define SIS_MAPREG 0x40
 	uint8_t new, newer;
 
 	/* Extended BIOS enable = 1, Lower BIOS Enable = 1 */
 	/* This is 0xFFF8000~0xFFFF0000 decoding on SiS 540/630. */
-	new = pci_read_byte(dev, 0x40);
+	new = pci_read_byte(dev, SIS_MAPREG);
 	new &= (~0x04); /* No idea why we clear bit 2. */
 	new |= 0xb; /* 0x3 for some chipsets, bit 7 seems to be don't care. */
-	rpci_write_byte(dev, 0x40, new);
-	newer = pci_read_byte(dev, 0x40);
-	if (newer != new) {
-		msg_pinfo("Setting register 0x%x to 0x%x on %s failed "
-			  "(WARNING ONLY).\n", 0x40, new, name);
-		msg_pinfo("Stuck at 0x%x\n", newer);
+	rpci_write_byte(dev, SIS_MAPREG, new);
+	newer = pci_read_byte(dev, SIS_MAPREG);
+	if (newer != new) { /* FIXME: share this with other code? */
+		msg_pinfo("Setting register 0x%x to 0x%02x on %s failed (WARNING ONLY).\n",
+			  SIS_MAPREG, new, name);
+		msg_pinfo("Stuck at 0x%02x.\n", newer);
 		return -1;
 	}
 	return 0;
@@ -176,8 +177,9 @@
 	return ret;
 }
 
-static int enable_flash_sis530(struct pci_dev *dev, const char *name)
+static int enable_flash_sis5x0(struct pci_dev *dev, const char *name, uint8_t dis_mask, uint8_t en_mask)
 {
+	#define SIS_REG 0x45
 	uint8_t new, newer;
 	int ret = 0;
 	struct pci_dev *sbdev;
@@ -188,46 +190,28 @@
 
 	ret = enable_flash_sis_mapping(sbdev, name);
 
-	new = pci_read_byte(sbdev, 0x45);
-	new &= (~0x20);
-	new |= 0x4;
-	rpci_write_byte(sbdev, 0x45, new);
-	newer = pci_read_byte(sbdev, 0x45);
-	if (newer != new) {
-		msg_pinfo("Setting register 0x%x to 0x%x on %s failed "
-			  "(WARNING ONLY).\n", 0x45, new, name);
-		msg_pinfo("Stuck at 0x%x\n", newer);
+	new = pci_read_byte(sbdev, SIS_REG);
+	new &= (~dis_mask);
+	new |= en_mask;
+	rpci_write_byte(sbdev, SIS_REG, new);
+	newer = pci_read_byte(sbdev, SIS_REG);
+	if (newer != new) { /* FIXME: share this with other code? */
+		msg_pinfo("Setting register 0x%x to 0x%02x on %s failed (WARNING ONLY).\n", SIS_REG, new, name);
+		msg_pinfo("Stuck at 0x%02x\n", newer);
 		ret = -1;
 	}
 
 	return ret;
 }
 
+static int enable_flash_sis530(struct pci_dev *dev, const char *name)
+{
+	return enable_flash_sis5x0(dev, name, 0x20, 0x04);
+}
+
 static int enable_flash_sis540(struct pci_dev *dev, const char *name)
 {
-	uint8_t new, newer;
-	int ret = 0;
-	struct pci_dev *sbdev;
-
-	sbdev = find_southbridge(dev->vendor_id, name);
-	if (!sbdev)
-		return -1;
-
-	ret = enable_flash_sis_mapping(sbdev, name);
-
-	new = pci_read_byte(sbdev, 0x45);
-	new &= (~0x80);
-	new |= 0x40;
-	rpci_write_byte(sbdev, 0x45, new);
-	newer = pci_read_byte(sbdev, 0x45);
-	if (newer != new) {
-		msg_pinfo("Setting register 0x%x to 0x%x on %s failed "
-			  "(WARNING ONLY).\n", 0x45, new, name);
-		msg_pinfo("Stuck at 0x%x\n", newer);
-		ret = -1;
-	}
-
-	return ret;
+	return enable_flash_sis5x0(dev, name, 0x80, 0x40);
 }
 
 /* Datasheet:
@@ -268,9 +252,8 @@
 
 	rpci_write_word(dev, xbcs, new);
 
-	if (pci_read_word(dev, xbcs) != new) {
-		msg_pinfo("Setting register 0x%x to 0x%x on %s failed "
-			  "(WARNING ONLY).\n", xbcs, new, name);
+	if (pci_read_word(dev, xbcs) != new) { /* FIXME: share this with other code? */
+		msg_pinfo("Setting register 0x%04x to 0x%04x on %s failed (WARNING ONLY).\n", xbcs, new, name);
 		return -1;
 	}
 
@@ -678,8 +661,7 @@
 	rpci_write_byte(dev, 0x40, val);
 
 	if (pci_read_byte(dev, 0x40) != val) {
-		msg_pinfo("\nWARNING: Failed to enable flash write on \"%s\"\n",
-			  name);
+		msg_pinfo("\nWARNING: Failed to enable flash write on \"%s\"\n", name);
 		return -1;
 	}
 
@@ -851,15 +833,15 @@
 
 static int enable_flash_sc1100(struct pci_dev *dev, const char *name)
 {
+	#define SC_REG 0x52
 	uint8_t new;
 
-	rpci_write_byte(dev, 0x52, 0xee);
+	rpci_write_byte(dev, SC_REG, 0xee);
 
-	new = pci_read_byte(dev, 0x52);
+	new = pci_read_byte(dev, SC_REG);
 
-	if (new != 0xee) {
-		msg_pinfo("Setting register 0x%x to 0x%x on %s failed "
-			  "(WARNING ONLY).\n", 0x52, new, name);
+	if (new != 0xee) { /* FIXME: share this with other code? */
+		msg_pinfo("Setting register 0x%x to 0x%02x on %s failed (WARNING ONLY).\n", SC_REG, new, name);
 		return -1;
 	}
 
@@ -869,29 +851,31 @@
 /* Works for AMD-8111, VIA VT82C586A/B, VIA VT82C686A/B. */
 static int enable_flash_amd8111(struct pci_dev *dev, const char *name)
 {
+	#define AMD_MAPREG 0x43
+	#define AMD_ENREG 0x40
 	uint8_t old, new;
 
 	/* Enable decoding at 0xffb00000 to 0xffffffff. */
-	old = pci_read_byte(dev, 0x43);
+	old = pci_read_byte(dev, AMD_MAPREG);
 	new = old | 0xC0;
 	if (new != old) {
-		rpci_write_byte(dev, 0x43, new);
-		if (pci_read_byte(dev, 0x43) != new) {
-			msg_pinfo("Setting register 0x%x to 0x%x on %s failed "
-				  "(WARNING ONLY).\n", 0x43, new, name);
+		rpci_write_byte(dev, AMD_MAPREG, new);
+		if (pci_read_byte(dev, AMD_MAPREG) != new) {
+			msg_pinfo("Setting register 0x%x to 0x%02x on %s failed (WARNING ONLY).\n",
+				  AMD_MAPREG, new, name);
 		}
 	}
 
 	/* Enable 'ROM write' bit. */
-	old = pci_read_byte(dev, 0x40);
+	old = pci_read_byte(dev, AMD_ENREG);
 	new = old | 0x01;
 	if (new == old)
 		return 0;
-	rpci_write_byte(dev, 0x40, new);
+	rpci_write_byte(dev, AMD_ENREG, new);
 
-	if (pci_read_byte(dev, 0x40) != new) {
-		msg_pinfo("Setting register 0x%x to 0x%x on %s failed "
-			  "(WARNING ONLY).\n", 0x40, new, name);
+	if (pci_read_byte(dev, AMD_ENREG) != new) {
+		msg_pinfo("Setting register 0x%x to 0x%02x on %s failed (WARNING ONLY).\n",
+			  AMD_ENREG, new, name);
 		return -1;
 	}
 
@@ -1055,8 +1039,8 @@
 	new = old | 0xC0;
 	if (new != old) {
 		rpci_write_byte(dev, reg, new);
-		if (pci_read_byte(dev, reg) != new) {
-			msg_pinfo("Setting register 0x%02x to 0x%x on %s failed.\n", reg, new, name);
+		if (pci_read_byte(dev, reg) != new) { /* FIXME: share this with other code? */
+			msg_pinfo("Setting register 0x%02x to 0x%02x on %s failed.\n", reg, new, name);
 			err++;
 		}
 	}
@@ -1298,7 +1282,7 @@
 	{0x1039, 0x0645, NT, "SiS", "645",		enable_flash_sis540},
 	{0x1039, 0x0646, OK, "SiS", "645DX",		enable_flash_sis540},
 	{0x1039, 0x0648, NT, "SiS", "648",		enable_flash_sis540},
-	{0x1039, 0x0650, NT, "SiS", "650",		enable_flash_sis540},
+	{0x1039, 0x0650, OK, "SiS", "650",		enable_flash_sis540},
 	{0x1039, 0x0651, OK, "SiS", "651",		enable_flash_sis540},
 	{0x1039, 0x0655, NT, "SiS", "655",		enable_flash_sis540},
 	{0x1039, 0x0661, OK, "SiS", "661",		enable_flash_sis540},
@@ -1355,7 +1339,7 @@
 	{0x10de, 0x0442, NT, "NVIDIA", "MCP65",		enable_flash_mcp6x_7x},
 	{0x10de, 0x0443, NT, "NVIDIA", "MCP65",		enable_flash_mcp6x_7x},
 	{0x10de, 0x0548, OK, "NVIDIA", "MCP67",		enable_flash_mcp6x_7x},
-	{0x10de, 0x075c, NT, "NVIDIA", "MCP78S",	enable_flash_mcp6x_7x},
+	{0x10de, 0x075c, OK, "NVIDIA", "MCP78S",	enable_flash_mcp6x_7x},
 	{0x10de, 0x075d, OK, "NVIDIA", "MCP78S",	enable_flash_mcp6x_7x},
 	{0x10de, 0x07d7, OK, "NVIDIA", "MCP73",		enable_flash_mcp6x_7x},
 	{0x10de, 0x0aac, OK, "NVIDIA", "MCP79",		enable_flash_mcp6x_7x},