goldfish/tty.c: Add device version register

qemu2 (ranchu) has introduced changes to the way this
device handles IO operations, it uses physical instead
of virtual addresses. Changes have been made to the
driver as well on android-goldfish-3.10 branch.

The driver now assumes that this device implements
versioning register on offset 0x20 :

TTY_VERSION        = 0x20

It uses this information to detect which platform it's
running on (Goldfish or Ranchu) and pass on virtual
or physical addresses accordingly.

And a little bit of estetic cleanup.

Change-Id: I219c895a49f9a3dbac3b9c0cd003152e5ba5a36d
diff --git a/hw/android/goldfish/tty.c b/hw/android/goldfish/tty.c
index 9e04c6c..5afc2d0 100644
--- a/hw/android/goldfish/tty.c
+++ b/hw/android/goldfish/tty.c
@@ -24,6 +24,9 @@
     TTY_DATA_LEN       = 0x14,
     TTY_DATA_PTR_HIGH  = 0x18,
 
+    /* Only used by kernel 3.10+ */
+    TTY_VERSION        = 0x20,
+
     TTY_CMD_INT_DISABLE    = 0,
     TTY_CMD_INT_ENABLE     = 1,
     TTY_CMD_WRITE_BUFFER   = 2,
@@ -40,8 +43,22 @@
     uint32_t data_count;
 };
 
+#define  TTY_DEVICE_VERSION 0
 #define  GOLDFISH_TTY_SAVE_VERSION  2
 
+#define  DEBUG 0
+
+/* Set to 1 to debug i/o register reads/writes */
+#define DEBUG_REGS  0
+
+#if DEBUG >= 1
+#  define D(...)  fprintf(stderr, __VA_ARGS__)
+#else
+#  define D(...)  (void)0
+#endif
+
+#define E(...)  cpu_abort(cpu_single_env, __VA_ARGS__)
+
 static void  goldfish_tty_save(QEMUFile*  f, void*  opaque)
 {
     struct tty_state*  s = opaque;
@@ -78,15 +95,15 @@
 {
     struct tty_state *s = (struct tty_state *)opaque;
 
-    //printf("goldfish_tty_read %x %x\n", offset, size);
+    D("goldfish_tty_read %" HWADDR_PRIx "\n", offset);
 
     switch (offset) {
         case TTY_BYTES_READY:
             return s->data_count;
+        case TTY_VERSION:
+            return TTY_DEVICE_VERSION;
     default:
-        cpu_abort(cpu_single_env,
-                  "goldfish_tty_read: Bad offset %" HWADDR_PRIx "\n",
-                  offset);
+        E("goldfish_tty_read: Bad offset %" HWADDR_PRIx "\n", offset);
         return 0;
     }
 }
@@ -95,7 +112,7 @@
 {
     struct tty_state *s = (struct tty_state *)opaque;
 
-    //printf("goldfish_tty_write %x %x %x\n", offset, value, size);
+    D("goldfish_tty_write %" HWADDR_PRIx " %x\n", offset, value);
 
     switch(offset) {
         case TTY_PUT_CHAR: {
@@ -141,15 +158,15 @@
                             buf += to_write;
                             len -= to_write;
                         }
-                        //printf("goldfish_tty_write: got %d bytes from %llx\n", s->ptr_len, (unsigned long long)s->ptr);
+                        D("goldfish_tty_write: got %d bytes from %llx\n", s->ptr_len, (unsigned long long)s->ptr);
                     }
                     break;
 
                 case TTY_CMD_READ_BUFFER:
                     if(s->ptr_len > s->data_count)
-                        cpu_abort (cpu_single_env, "goldfish_tty_write: reading more data than available %d %d\n", s->ptr_len, s->data_count);
+                        E("goldfish_tty_write: reading more data than available %d %d\n", s->ptr_len, s->data_count);
                     safe_memory_rw_debug(current_cpu, s->ptr, s->data, s->ptr_len,1);
-                    //printf("goldfish_tty_write: read %d bytes to %llx\n", s->ptr_len, (unsigned long long)s->ptr);
+                    D("goldfish_tty_write: read %d bytes to %llx\n", s->ptr_len, (unsigned long long)s->ptr);
                     if(s->data_count > s->ptr_len)
                         memmove(s->data, s->data + s->ptr_len, s->data_count - s->ptr_len);
                     s->data_count -= s->ptr_len;
@@ -158,7 +175,7 @@
                     break;
 
                 default:
-                    cpu_abort (cpu_single_env, "goldfish_tty_write: Bad command %x\n", value);
+                    E("goldfish_tty_write: Bad command %x\n", value);
             };
             break;
 
@@ -175,9 +192,7 @@
             break;
 
         default:
-            cpu_abort(cpu_single_env,
-                      "goldfish_tty_write: Bad offset %" HWADDR_PRIx "\n",
-                      offset);
+            E("goldfish_tty_write: Bad offset %" HWADDR_PRIx "\n", offset);
     }
 }