Provide support for setting up the framebuffer from EDID

Add three functions to edid.c:

void set_vbe_mode_info_valid(struct edid *edid, uintptr_t fb_addr)
takes an edid and uintptr_t, and fills in a static lb_framebuffer struct
as well as setting the static vbe_valid to 1 unless some problem
is found in the edid. The intent here is that this could be called from
the native graphics setup code on both ARM and x86.

int vbe_mode_info_valid(void)
returns value of the static vbe_valid.

void fill_lb_framebuffer(struct lb_framebuffer *framebuffer)
copies the static edid_fb to lb_framebuffer.

There is now a common vbe.h in src/include, removed the two special ones.

In general, graphics in coreboot is a mess, but graphics is always a
mess.  We don't have a clean way to try two different ways to turn on
a device and use the one that works. One battle at a time. Overall,
things are much better.

The best part: this code would also work for ARM, which also uses EDID.

Change-Id: Id23eb61498b331d44ab064b8fb4cb10f07cff7f3
Signed-off-by: Ronald G. Minnich <rminnich@google.com>
Signed-off-by: Gabe Black <gabeblack@chromium.org>
Reviewed-on: http://review.coreboot.org/3636
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/src/device/oprom/realmode/x86.c b/src/device/oprom/realmode/x86.c
index 94b65e1..338294d 100644
--- a/src/device/oprom/realmode/x86.c
+++ b/src/device/oprom/realmode/x86.c
@@ -29,7 +29,7 @@
 #include <delay.h>
 #include <pc80/i8259.h>
 #include "x86.h"
-#include "vbe.h"
+#include <vbe.h>
 #include <lib/jpeg.h>
 /* we use x86emu's register file representation */
 #include <x86emu/regs.h>
diff --git a/src/device/oprom/yabel/compat/functions.c b/src/device/oprom/yabel/compat/functions.c
index f693d7b..f0160ed 100644
--- a/src/device/oprom/yabel/compat/functions.c
+++ b/src/device/oprom/yabel/compat/functions.c
@@ -18,7 +18,7 @@
 #include <device/device.h>
 #include "../debug.h"
 #include "../biosemu.h"
-#include "../vbe.h"
+#include <vbe.h>
 #include "../compat/time.h"
 
 #define VMEM_SIZE (1024 * 1024) /* 1 MB */
diff --git a/src/device/oprom/yabel/vbe.h b/src/device/oprom/yabel/vbe.h
deleted file mode 100644
index bf286bc..0000000
--- a/src/device/oprom/yabel/vbe.h
+++ /dev/null
@@ -1,23 +0,0 @@
-/******************************************************************************
- * Copyright (c) 2004, 2008 IBM Corporation
- * All rights reserved.
- * This program and the accompanying materials
- * are made available under the terms of the BSD License
- * which accompanies this distribution, and is available at
- * http://www.opensource.org/licenses/bsd-license.php
- *
- * Contributors:
- *     IBM Corporation - initial implementation
- *****************************************************************************/
-
-#ifndef _BIOSEMU_VBE_H_
-#define _BIOSEMU_VBE_H_
-
-struct lb_framebuffer;
-
-void vbe_set_graphics(void);
-int vbe_mode_info_valid(void);
-void fill_lb_framebuffer(struct lb_framebuffer *framebuffer);
-void vbe_textmode_console(void);
-
-#endif
diff --git a/src/include/edid.h b/src/include/edid.h
index 6d76bb7..8809cce 100644
--- a/src/include/edid.h
+++ b/src/include/edid.h
@@ -36,6 +36,8 @@
 	unsigned int nonconformant;
 	unsigned int type;
 	unsigned int bpp;
+	unsigned int xres;
+	unsigned int yres;
 	unsigned int voltage;
 	unsigned int sync;
 	unsigned int xsize_cm;
@@ -62,5 +64,7 @@
 	const char *stereo;
 };
 
+/* Defined in src/lib/edid.c */
 int decode_edid(unsigned char *edid, int size, struct edid *out);
+
 #endif /* EDID_H */
diff --git a/src/device/oprom/include/vbe.h b/src/include/vbe.h
similarity index 94%
rename from src/device/oprom/include/vbe.h
rename to src/include/vbe.h
index ab26d59..8ad9d2e 100644
--- a/src/device/oprom/include/vbe.h
+++ b/src/include/vbe.h
@@ -10,7 +10,10 @@
  * Contributors:
  *     IBM Corporation - initial implementation
  *****************************************************************************/
+#ifndef VBE_H
+#define VBE_H
 
+#include <boot/coreboot_tables.h>
 // these structs are for input from and output to OF
 typedef struct {
 	u8 display_type;	// 0=NONE, 1= analog, 2=digital
@@ -102,16 +105,13 @@
 	u8 edid_block_zero[128];
 } vbe_ddc_info_t;
 
-struct lb_framebuffer;
-
-void vbe_set_graphics(void);
-// A way to check if mode information collected by vbe_set_graphics is valid
-// and fill_lb_framebuffer will have real information to use.
-int vbe_mode_info_valid(void);
-void vbe_textmode_console(void);
-void fill_lb_framebuffer(struct lb_framebuffer *framebuffer);
-
 #define VESA_GET_INFO		0x4f00
 #define VESA_GET_MODE_INFO	0x4f01
 #define VESA_SET_MODE		0x4f02
 
+int vbe_mode_info_valid(void);
+void fill_lb_framebuffer(struct lb_framebuffer *framebuffer);
+void vbe_set_graphics(void);
+void vbe_textmode_console(void);
+
+#endif // VBE_H
diff --git a/src/lib/edid.c b/src/lib/edid.c
index a794489..c4c5e45 100644
--- a/src/lib/edid.c
+++ b/src/lib/edid.c
@@ -36,6 +36,8 @@
 #include <string.h>
 #include <stdlib.h>
 #include <edid.h>
+#include <boot/coreboot_tables.h>
+#include <vbe.h>
 
 static int claims_one_point_oh = 0;
 static int claims_one_point_two = 0;
@@ -68,6 +70,9 @@
 
 static int conformant = 1;
 
+static int vbe_valid;
+static struct lb_framebuffer edid_fb;
+
 static char *manufacturer_name(struct edid *out, unsigned char *x)
 {
 	out->manuf_name[0] = ((x[0] & 0x7C) >> 2) + '@';
@@ -1226,7 +1231,7 @@
 		}
 
 		printk(BIOS_SPEW, "Checksum\n");
-		do_checksum(edid);
+		vbe_valid = do_checksum(edid);
 		for(i = 0; i < size; i += 128)
 			nonconformant_extension = parse_extension(out, &edid[i]);
 /*
@@ -1386,3 +1391,67 @@
  * SPWG also says something strange about the LSB of detailed descriptor 1:
  * "LSB is set to "1" if panel is DE-timing only. H/V can be ignored."
  */
+/*
+ * Take an edid, and create a framebuffer. Set vbe_valid to 1.
+ */
+
+void set_vbe_mode_info_valid(struct edid *edid, uintptr_t fb_addr);
+void set_vbe_mode_info_valid(struct edid *edid, uintptr_t fb_addr)
+{
+	edid_fb.physical_address = fb_addr;
+	edid_fb.x_resolution = edid->ha;
+	edid_fb.y_resolution = edid->va;
+	/* In the case of (e.g.) 24bpp, the convention nowadays
+	 * seems to be to round it up to the nearest reasonable
+	 * boundary, because otherwise the byte-packing is hideous.
+	 * So, for example, in RGB with no alpha, the bytes are still
+	 * packed into 32-bit words, the so-called 32bpp-no-alpha mode.
+	 * Or, in 5:6:5 mode, the bytes are also packed into 32-bit words,
+	 * and in 4:4:4 mode, they are packed into 16-bit words.
+	 * Good call on the hardware guys part.
+	 * It's not clear we're covering all cases here, but
+	 * I'm not sure with grahpics you ever can.
+	 */
+	edid_fb.bits_per_pixel = edid->bpp;
+	switch(edid->bpp){
+	case 32:
+	case 24:
+		/* packed into 4-byte words */
+		edid_fb.bytes_per_line = edid->ha * 4;
+		edid_fb.red_mask_pos = 16;
+		edid_fb.red_mask_size = 8;
+		edid_fb.green_mask_pos = 8;
+		edid_fb.green_mask_size = 8;
+		edid_fb.blue_mask_pos = 0;
+		edid_fb.blue_mask_size = 8;
+		break;
+	case 16:
+		/* packed into 2-byte words */
+		edid_fb.bytes_per_line = edid->ha * 2;
+		edid_fb.red_mask_pos = 12;
+		edid_fb.red_mask_size = 4;
+		edid_fb.green_mask_pos = 8;
+		edid_fb.green_mask_size = 4;
+		edid_fb.blue_mask_pos = 0;
+		edid_fb.blue_mask_size = 4;
+		break;
+	default:
+		printk(BIOS_SPEW, "%s: unsupported BPP %d\n", __func__,
+		       edid->bpp);
+		return;
+	}
+
+	edid_fb.reserved_mask_pos = 0;
+	edid_fb.reserved_mask_size = 0;
+	vbe_valid = 1;
+}
+
+int vbe_mode_info_valid(void)
+{
+	return vbe_valid;
+}
+
+void fill_lb_framebuffer(struct lb_framebuffer *framebuffer)
+{
+	*framebuffer = edid_fb;
+}
diff --git a/src/mainboard/google/snow/ramstage.c b/src/mainboard/google/snow/ramstage.c
index 72f830d..93979b2 100644
--- a/src/mainboard/google/snow/ramstage.c
+++ b/src/mainboard/google/snow/ramstage.c
@@ -42,40 +42,6 @@
 #define DRAM_SIZE	CONFIG_DRAM_SIZE_MB
 #define DRAM_END	(DRAM_START + DRAM_SIZE)	/* plus one... */
 
-int vbe_mode_info_valid(void);
-int vbe_mode_info_valid(void)
-{
-	return 1;
-}
-
-void fill_lb_framebuffer(struct lb_framebuffer *framebuffer);
-void fill_lb_framebuffer(struct lb_framebuffer *framebuffer)
-{
-	/*
-	 * The address returned points at the LCD colormap base. The
-	 * 64KiB offset points at the LCD base.
-	 */
-	framebuffer->physical_address =
-		(u32)cbmem_find(CBMEM_ID_CONSOLE) + 64*KiB;
-	printk(BIOS_SPEW, "%s: framebuffer->physical address is 0x%llx\n",
-			__func__, framebuffer->physical_address);
-
-	framebuffer->x_resolution = 1366;
-	framebuffer->y_resolution = 768;
-	framebuffer->bits_per_pixel = 16;
-	framebuffer->bytes_per_line =
-		(framebuffer->x_resolution * framebuffer->bits_per_pixel) / 8;
-
-	framebuffer->red_mask_pos = 11;
-	framebuffer->red_mask_size = 5;
-	framebuffer->green_mask_pos = 6;
-	framebuffer->green_mask_size = 5;
-	framebuffer->blue_mask_pos = 0;
-	framebuffer->blue_mask_size = 5;
-	framebuffer->reserved_mask_pos = 0;
-	framebuffer->reserved_mask_size = 0;
-}
-
 
 void hardwaremain(void);
 void main(void)
diff --git a/src/mainboard/intel/wtm2/i915.c b/src/mainboard/intel/wtm2/i915.c
index 8103016..9d0c27d 100644
--- a/src/mainboard/intel/wtm2/i915.c
+++ b/src/mainboard/intel/wtm2/i915.c
@@ -139,42 +139,96 @@
 
 static int i915_init_done = 0;
 
-int vbe_mode_info_valid(void)
+/* fill the palette. This runs when the P opcode is hit. */
+static void palette(void)
 {
-	return i915_init_done;
+	int i;
+	unsigned long color = 0;
+
+	for(i = 0; i < 256; i++, color += 0x010101){
+		io_i915_WRITE32(color, _LGC_PALETTE_A + (i<<2));
+	}
 }
 
-void fill_lb_framebuffer(struct lb_framebuffer *framebuffer)
-{
-	printk(BIOS_SPEW, "fill_lb_framebuffer: graphics is %p\n",
-	       (void *)graphics);
-	/* Please note: these will be filled from EDID.
-	 * these values are a placeholder.
-	 */
-	framebuffer->physical_address = graphics;
-	/* these are a fantasy, but will be fixed once we're getting
-	 * info from the hardware. Hard to get from the device tree,
-	 * which is arguably a defect of the device tree. Bear with me,
-	 * this Will Get Fixed.
-	 */
-	framebuffer->x_resolution = 1960;
-	framebuffer->y_resolution = 1700;
-	framebuffer->bytes_per_line = 1960*4;
-	framebuffer->bits_per_pixel = 32;
-	framebuffer->red_mask_pos = 16;
-	framebuffer->red_mask_size = 8;
-	framebuffer->green_mask_pos = 8;
-	framebuffer->green_mask_size = 8;
-	framebuffer->blue_mask_pos = 0;
-	framebuffer->blue_mask_size = 8;
-	framebuffer->reserved_mask_pos = 0;
-	framebuffer->reserved_mask_size = 0;
+static unsigned long times[4096];
 
+static int run(int index)
+{
+	int i, prev = 0;
+	struct iodef *id, *lastidread = 0;
+	unsigned long u, t;
+	if (index >= niodefs)
+		return index;
+	/* state machine! */
+	for(i = index, id = &iodefs[i]; id->op; i++, id++){
+		switch(id->op){
+		case M:
+			if (verbose & vmsg) printk(BIOS_SPEW, "%ld: %s\n",
+						   globalmicroseconds(), id->msg);
+			break;
+		case P:
+			palette();
+			break;
+		case R:
+			u = READ32(id->addr);
+			if (verbose & vio)
+				printk(BIOS_SPEW, "\texpect %08lx\n", id->data);
+			/* we're looking for something. */
+			if (lastidread->addr == id->addr){
+				/* they're going to be polling.
+				 * just do it 1000 times
+				 */
+				for(t = 0; t < 1000 && id->data != u; t++){
+					u = READ32(id->addr);
+				}
+				if (verbose & vspin) printk(BIOS_SPEW,
+							    "%s: # loops %ld got %08lx want %08lx\n",
+							    regname(id->addr),
+							    t, u, id->data);
+			}
+			lastidread = id;
+			break;
+		case W:
+			WRITE32(id->data, id->addr);
+			if (id->addr == PCH_PP_CONTROL){
+				if (verbose & vio)
+					printk(BIOS_SPEW, "PCH_PP_CONTROL\n");
+				switch(id->data & 0xf){
+				case 8: break;
+				case 7: break;
+				default: udelay(100000);
+					if (verbose & vio)
+						printk(BIOS_SPEW, "U %d\n", 100000);
+				}
+			}
+			break;
+		case V:
+			if (id->count < 8){
+				prev = verbose;
+				verbose = id->count;
+			} else {
+				verbose = prev;
+			}
+			printk(BIOS_SPEW, "Change verbosity to %d\n", verbose);
+			break;
+		case I:
+			printk(BIOS_SPEW, "run: return %d\n", i+1);
+			return i+1;
+			break;
+		default:
+			printk(BIOS_SPEW, "BAD TABLE, opcode %d @ %d\n", id->op, i);
+			return -1;
+		}
+		if (id->udelay)
+			udelay(id->udelay);
+		if (i < ARRAY_SIZE(times))
+			times[i] = globalmicroseconds();
+	}
+	printk(BIOS_SPEW, "run: return %d\n", i);
+	return i+1;
 }
 
-int i915lightup(unsigned int physbase,
-		unsigned int iobase,
-		unsigned int mmio,
+int i915lightup(unsigned int physbase, unsigned int iobase, unsigned int mmio,
 		unsigned int gfx);
 
 int i915lightup(unsigned int pphysbase, unsigned int piobase,