YABEL update
[coreboot.git] / util / x86emu / yabel / compat / functions.c
1 /****************************************************************************
2  * YABEL BIOS Emulator
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Copyright (c) 2008 Pattrick Hueper <phueper@hueper.net>
10  ****************************************************************************/
11
12 /* this file contains functions provided by SLOF, that the current biosemu implementation needs
13  * they should go away  inthe future...
14  */
15
16 #include <types.h>
17 #include <string.h>
18 #include <device/device.h>
19 #include "../debug.h"
20
21 #define VMEM_SIZE (1024 * 1024) /* 1 MB */
22
23 #if !defined(CONFIG_YABEL_DIRECTHW) || (!CONFIG_YABEL_DIRECTHW)
24 #ifdef CONFIG_YABEL_VIRTMEM_LOCATION
25 u8* vmem = (u8 *) CONFIG_YABEL_VIRTMEM_LOCATION;
26 #else
27 u8* vmem = (u8 *) (16*1024*1024); /* default to 16MB */
28 #endif
29 #else
30 u8* vmem = NULL;
31 #endif
32
33 u32 biosemu(u8 *biosmem, u32 biosmem_size, struct device *dev,
34             unsigned long rom_addr);
35 #if CONFIG_BOOTSPLASH
36 void vbe_set_graphics(void);
37 #endif
38
39 void run_bios(struct device * dev, unsigned long addr)
40 {
41
42         biosemu(vmem, VMEM_SIZE, dev, addr);
43
44 #if CONFIG_BOOTSPLASH
45         vbe_set_graphics();
46 #endif
47
48         if (vmem != NULL) {
49                 printf("Copying legacy memory from 0x%08x to the lower 1MB\n", vmem);
50                 memcpy(0x00000, vmem + 0x00000, 0x400);         // IVT
51                 memcpy(0x00400, vmem + 0x00400, 0x100);         // BDA
52                 memcpy(0xc0000, vmem + 0xc0000, 0x10000);       // VGA OPROM
53         }
54 }
55
56 u64 get_time(void)
57 {
58     u64 act;
59     u32 eax, edx;
60
61     __asm__ __volatile__(
62         "rdtsc"
63         : "=a"(eax), "=d"(edx)
64         : /* no inputs, no clobber */);
65     act = ((u64) edx << 32) | eax; 
66     return act;
67 }
68
69 unsigned int
70 read_io(void *addr, size_t sz)
71 {
72         unsigned int ret;
73         /* since we are using inb instructions, we need the port number as 16bit value */
74         u16 port = (u16)(u32) addr;
75
76         switch (sz) {
77         case 1:
78                 asm volatile ("inb %1, %b0" : "=a"(ret) : "d" (port));
79                 break;
80         case 2:
81                 asm volatile ("inw %1, %w0" : "=a"(ret) : "d" (port));
82                 break;
83         case 4:
84                 asm volatile ("inl %1, %0" : "=a"(ret) : "d" (port));
85                 break;
86         default:
87                 ret = 0;
88         }
89
90         return ret;
91 }
92
93 int
94 write_io(void *addr, unsigned int value, size_t sz)
95 {
96         u16 port = (u16)(u32) addr;
97         switch (sz) {
98         /* since we are using inb instructions, we need the port number as 16bit value */
99         case 1:
100                 asm volatile ("outb %b0, %1" : : "a"(value), "d" (port));
101                 break;
102         case 2:
103                 asm volatile ("outw %w0, %1" : : "a"(value), "d" (port));
104                 break;
105         case 4:
106                 asm volatile ("outl %0, %1" : : "a"(value), "d" (port));
107                 break;
108         default:
109                 return -1;
110         }
111
112         return 0;
113 }
114