Add IT8212F as programmer

This PCI PATA controller can use 3V parallel flash up to 128 kB.

My card was identified as:
  PCI 1283:8212, subsystem 1283:0001.

and labelled as:
  Innovision Multimedia LTD.
  EIO ATA133 RAID
  (DM-8401 Ver A)

This particular card did not require setting of any GPIO signals to
enable flash writing. My card has Pm39LV512 in PLCC32 package without
socket.

Rebased by Stefan (automatic cleanup, some PCI changes, changed
enable bit handling). Committed with test state NT because the
rebased version was not tested on real hardware (yet).

Corresponding to flashrom svn r1812.

Signed-off-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
diff --git a/Makefile b/Makefile
index faa8e58..7f7aa92 100644
--- a/Makefile
+++ b/Makefile
@@ -197,6 +197,11 @@
 else
 override CONFIG_ATAVIA = no
 endif
+ifeq ($(CONFIG_IT8212), yes)
+UNSUPPORTED_FEATURES += CONFIG_IT8212=yes
+else
+override CONFIG_IT8212 = no
+endif
 ifeq ($(CONFIG_DRKAISER), yes)
 UNSUPPORTED_FEATURES += CONFIG_DRKAISER=yes
 else
@@ -431,6 +436,9 @@
 # Enable Linux spidev interface by default. We disable it on non-Linux targets.
 CONFIG_LINUX_SPI ?= yes
 
+# Always enable ITE IT8212F PATA controllers for now.
+CONFIG_IT8212 ?= yes
+
 # Disable wiki printing by default. It is only useful if you have wiki access.
 CONFIG_PRINT_WIKI ?= no
 
@@ -549,6 +557,12 @@
 NEED_PCI := yes
 endif
 
+ifeq ($(CONFIG_IT8212), yes)
+FEATURE_CFLAGS += -D'CONFIG_IT8212=1'
+PROGRAMMER_OBJS += it8212.o
+NEED_PCI := yes
+endif
+
 ifeq ($(CONFIG_FT2232_SPI), yes)
 # This is a totally ugly hack.
 FEATURE_CFLAGS += $(shell LC_ALL=C grep -q "FTDISUPPORT := yes" .features && printf "%s" "-D'CONFIG_FT2232_SPI=1'")
diff --git a/flashrom.8.tmpl b/flashrom.8.tmpl
index 956e4f5..0b4867d 100644
--- a/flashrom.8.tmpl
+++ b/flashrom.8.tmpl
@@ -194,6 +194,8 @@
 .sp
 .BR "* atavia" " (for flash ROMs on VIA VT6421A SATA controllers)"
 .sp
+.BR "* it8212" " (for flash ROMs on ITE IT8212F ATA/RAID controller)"
+.sp
 .BR "* ft2232_spi" " (for SPI flash ROMs attached to an FT2232/FT4232H/FT232H family \
 based USB SPI programmer), including the DLP Design DLP-USB1232H, \
 FTDI FT2232H Mini-Module, FTDI FT4232H Mini-Module, openbiosprog-spi, Amontec \
@@ -591,7 +593,7 @@
 .SS
 .BR "nic3com" , " nicrealtek" , " nicnatsemi" , " nicintel"\
 , " nicintel_spi" , " gfxnvidia" , " ogp_spi" , " drkaiser" , " satasii"\
-, " satamv" , " atahpt" ", and " atavia " programmers
+, " satamv" , " atahpt" ", " atavia " and " it8212 " programmers
 These programmers have an option to specify the PCI address of the card
 your want to use, which must be specified if more than one card supported
 by the selected programmer is installed in your system. The syntax is
@@ -914,7 +916,7 @@
 .B atahpt
 needs PCI configuration space access and raw I/O port access.
 .sp
-.BR gfxnvidia " and " drkaiser
+.BR gfxnvidia ", " drkaiser " and " it8212
 need PCI configuration space access and raw memory access.
 .sp
 .B rayer_spi
diff --git a/flashrom.c b/flashrom.c
index 8a2a5b1..a8a8533 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -184,6 +184,18 @@
 	},
 #endif
 
+#if CONFIG_IT8212 == 1
+	{
+		.name			= "it8212",
+		.type			= PCI,
+		.devs.dev		= devs_it8212,
+		.init			= it8212_init,
+		.map_flash_region	= fallback_map,
+		.unmap_flash_region	= fallback_unmap,
+		.delay			= internal_delay,
+	},
+#endif
+
 #if CONFIG_FT2232_SPI == 1
 	{
 		.name			= "ft2232_spi",
diff --git a/it8212.c b/it8212.c
new file mode 100644
index 0000000..f68121a
--- /dev/null
+++ b/it8212.c
@@ -0,0 +1,86 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2011 Carl-Daniel Hailfinger
+ * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#include <stdlib.h>
+#include "flash.h"
+#include "programmer.h"
+#include "hwaccess.h"
+
+static uint8_t *it8212_bar = NULL;
+
+#define PCI_VENDOR_ID_ITE 0x1283
+
+const struct dev_entry devs_it8212[] = {
+	{PCI_VENDOR_ID_ITE, 0x8212, OK, "ITE", "8212F PATA RAID"},
+
+	{},
+};
+
+#define IT8212_MEMMAP_SIZE (128 * 1024)
+#define IT8212_MEMMAP_MASK (IT8212_MEMMAP_SIZE - 1)
+
+static void it8212_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
+static uint8_t it8212_chip_readb(const struct flashctx *flash, const chipaddr addr);
+static const struct par_programmer par_programmer_it8212 = {
+		.chip_readb		= it8212_chip_readb,
+		.chip_readw		= fallback_chip_readw,
+		.chip_readl		= fallback_chip_readl,
+		.chip_readn		= fallback_chip_readn,
+		.chip_writeb		= it8212_chip_writeb,
+		.chip_writew		= fallback_chip_writew,
+		.chip_writel		= fallback_chip_writel,
+		.chip_writen		= fallback_chip_writen,
+};
+
+int it8212_init(void)
+{
+	if (rget_io_perms())
+		return 1;
+
+	struct pci_dev *dev = pcidev_init(devs_it8212, PCI_ROM_ADDRESS);
+	if (!dev)
+		return 1;
+
+	/* Bit 0 is address decode enable, 17-31 the base address, everything else reserved/zero. */
+	io_base_addr = pcidev_readbar(dev, PCI_ROM_ADDRESS) & 0xFFFFFFFE;
+	if (!io_base_addr)
+		return 1;
+
+	it8212_bar = rphysmap("IT8212F flash", io_base_addr, IT8212_MEMMAP_SIZE);
+	if (it8212_bar == ERROR_PTR)
+		return 1;
+
+	/* Restore ROM BAR decode state automatically at shutdown. */
+	rpci_write_long(dev, PCI_ROM_ADDRESS, io_base_addr | 0x01);
+
+	max_rom_decode.parallel = IT8212_MEMMAP_SIZE;
+	register_par_programmer(&par_programmer_it8212, BUS_PARALLEL);
+	return 0;
+}
+
+static void it8212_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
+{
+	pci_mmio_writeb(val, it8212_bar + (addr & IT8212_MEMMAP_MASK));
+}
+
+static uint8_t it8212_chip_readb(const struct flashctx *flash, const chipaddr addr)
+{
+	return pci_mmio_readb(it8212_bar + (addr & IT8212_MEMMAP_MASK));
+}
diff --git a/programmer.h b/programmer.h
index 02d0db8..3ea014c 100644
--- a/programmer.h
+++ b/programmer.h
@@ -57,6 +57,9 @@
 #if CONFIG_ATAVIA == 1
 	PROGRAMMER_ATAVIA,
 #endif
+#if CONFIG_IT8212 == 1
+	PROGRAMMER_IT8212,
+#endif
 #if CONFIG_FT2232_SPI == 1
 	PROGRAMMER_FT2232_SPI,
 #endif
@@ -444,6 +447,12 @@
 extern const struct dev_entry ata_via[];
 #endif
 
+/* it8212.c */
+#if CONFIG_IT8212 == 1
+int it8212_init(void);
+extern const struct dev_entry devs_it8212[];
+#endif
+
 /* ft2232_spi.c */
 #if CONFIG_FT2232_SPI == 1
 int ft2232_spi_init(void);