Have all programmer init functions register bus masters/programmers

All programmer types (Parallel, SPI, Opaque) now register themselves
into a generic programmer list and probing is now programmer-centric
instead of chip-centric.
Registering multiple SPI/... masters at the same time is now possible
without any problems. Handling multiple flash chips is still unchanged,
but now we have the infrastructure to deal with "dual BIOS" and "one
flash behind southbridge and one flash behind EC" sanely.

A nice side effect is that this patch kills quite a few global variables
and improves the situation for libflashrom.

Hint for developers:
struct {spi,par,opaque}_programmer now have a void *data pointer to
store any additional programmer-specific data, e.g. hardware
configuration info.

Note:
flashrom -f -c FOO -r forced_read.bin
does not work anymore. We have to find an architecturally clean way to
solve this.

Corresponding to flashrom svn r1475.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: Michael Karcher <flashrom@mkarcher.dialup.fu-berlin.de>
diff --git a/spi.c b/spi.c
index 02c83f7..b2d3eb0 100644
--- a/spi.c
+++ b/spi.c
@@ -1,7 +1,7 @@
 /*
  * This file is part of the flashrom project.
  *
- * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
+ * Copyright (C) 2007, 2008, 2009, 2010, 2011 Carl-Daniel Hailfinger
  * Copyright (C) 2008 coresystems GmbH
  *
  * This program is free software; you can redistribute it and/or modify
@@ -30,43 +30,17 @@
 #include "programmer.h"
 #include "spi.h"
 
-const struct spi_programmer spi_programmer_none = {
-	.type = SPI_CONTROLLER_NONE,
-	.max_data_read = MAX_DATA_UNSPECIFIED,
-	.max_data_write = MAX_DATA_UNSPECIFIED,
-	.command = NULL,
-	.multicommand = NULL,
-	.read = NULL,
-	.write_256 = NULL,
-};
-
-const struct spi_programmer *spi_programmer = &spi_programmer_none;
-
 int spi_send_command(struct flashctx *flash, unsigned int writecnt,
 		     unsigned int readcnt, const unsigned char *writearr,
 		     unsigned char *readarr)
 {
-	if (!spi_programmer->command) {
-		msg_perr("%s called, but SPI is unsupported on this "
-			 "hardware. Please report a bug at "
-			 "flashrom@flashrom.org\n", __func__);
-		return 1;
-	}
-
-	return spi_programmer->command(flash, writecnt, readcnt, writearr,
+	return flash->pgm->spi.command(flash, writecnt, readcnt, writearr,
 				       readarr);
 }
 
 int spi_send_multicommand(struct flashctx *flash, struct spi_command *cmds)
 {
-	if (!spi_programmer->multicommand) {
-		msg_perr("%s called, but SPI is unsupported on this "
-			 "hardware. Please report a bug at "
-			 "flashrom@flashrom.org\n", __func__);
-		return 1;
-	}
-
-	return spi_programmer->multicommand(flash, cmds);
+	return flash->pgm->spi.multicommand(flash, cmds);
 }
 
 int default_spi_send_command(struct flashctx *flash, unsigned int writecnt,
@@ -104,7 +78,7 @@
 int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start,
 		     unsigned int len)
 {
-	unsigned int max_data = spi_programmer->max_data_read;
+	unsigned int max_data = flash->pgm->spi.max_data_read;
 	if (max_data == MAX_DATA_UNSPECIFIED) {
 		msg_perr("%s called, but SPI read chunk size not defined "
 			 "on this hardware. Please report a bug at "
@@ -117,7 +91,7 @@
 int default_spi_write_256(struct flashctx *flash, uint8_t *buf,
 			  unsigned int start, unsigned int len)
 {
-	unsigned int max_data = spi_programmer->max_data_write;
+	unsigned int max_data = flash->pgm->spi.max_data_write;
 	if (max_data == MAX_DATA_UNSPECIFIED) {
 		msg_perr("%s called, but SPI write chunk size not defined "
 			 "on this hardware. Please report a bug at "
@@ -131,12 +105,6 @@
 		  unsigned int len)
 {
 	unsigned int addrbase = 0;
-	if (!spi_programmer->read) {
-		msg_perr("%s called, but SPI read is unsupported on this "
-			 "hardware. Please report a bug at "
-			 "flashrom@flashrom.org\n", __func__);
-		return 1;
-	}
 
 	/* Check if the chip fits between lowest valid and highest possible
 	 * address. Highest possible address with the current SPI implementation
@@ -157,7 +125,7 @@
 			 "access window.\n");
 		msg_perr("Read will probably return garbage.\n");
 	}
-	return spi_programmer->read(flash, buf, addrbase + start, len);
+	return flash->pgm->spi.read(flash, buf, addrbase + start, len);
 }
 
 /*
@@ -170,14 +138,7 @@
 int spi_chip_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start,
 		       unsigned int len)
 {
-	if (!spi_programmer->write_256) {
-		msg_perr("%s called, but SPI page write is unsupported on this "
-			 "hardware. Please report a bug at "
-			 "flashrom@flashrom.org\n", __func__);
-		return 1;
-	}
-
-	return spi_programmer->write_256(flash, buf, start, len);
+	return flash->pgm->spi.write_256(flash, buf, start, len);
 }
 
 /*
@@ -187,7 +148,7 @@
  */
 uint32_t spi_get_valid_read_addr(struct flashctx *flash)
 {
-	switch (spi_programmer->type) {
+	switch (flash->pgm->spi.type) {
 #if CONFIG_INTERNAL == 1
 #if defined(__i386__) || defined(__x86_64__)
 	case SPI_CONTROLLER_ICH7:
@@ -200,8 +161,22 @@
 	}
 }
 
-void register_spi_programmer(const struct spi_programmer *pgm)
+int register_spi_programmer(const struct spi_programmer *pgm)
 {
-	spi_programmer = pgm;
-	buses_supported |= BUS_SPI;
+	struct registered_programmer rpgm;
+
+	if (!pgm->write_256 || !pgm->read || !pgm->command ||
+	    !pgm->multicommand ||
+	    ((pgm->command == default_spi_send_command) &&
+	     (pgm->multicommand == default_spi_send_multicommand))) {
+		msg_perr("%s called with incomplete programmer definition. "
+			 "Please report a bug at flashrom@flashrom.org\n",
+			 __func__);
+		return ERROR_FLASHROM_BUG;
+	}
+
+
+	rpgm.buses_supported = BUS_SPI;
+	rpgm.spi = *pgm;
+	return register_programmer(&rpgm);
 }