bf3376d4f6000dc4ad16865d3b08bef85a06fbb1
[qemu] / qemu / hw / mips_r4k.c
1 #include "vl.h"
2
3 #define BIOS_FILENAME "mips_bios.bin"
4 //#define BIOS_FILENAME "system.bin"
5 #define KERNEL_LOAD_ADDR 0x80010000
6 #define INITRD_LOAD_ADDR 0x80800000
7
8 extern FILE *logfile;
9
10 static PITState *pit;
11
12 static void pic_irq_request(void *opaque, int level)
13 {
14     if (level) {
15         cpu_single_env->CP0_Cause |= 0x00000400;
16         cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
17     } else {
18         cpu_single_env->CP0_Cause &= ~0x00000400;
19         cpu_reset_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
20     }
21 }
22
23 void cpu_mips_irqctrl_init (void)
24 {
25 }
26
27 uint32_t cpu_mips_get_random (CPUState *env)
28 {
29     uint32_t now = qemu_get_clock(vm_clock);
30
31     return now % (MIPS_TLB_NB - env->CP0_Wired) + env->CP0_Wired;
32 }
33
34 /* MIPS R4K timer */
35 uint32_t cpu_mips_get_count (CPUState *env)
36 {
37     return env->CP0_Count +
38         (uint32_t)muldiv64(qemu_get_clock(vm_clock),
39                            100 * 1000 * 1000, ticks_per_sec);
40 }
41
42 static void cpu_mips_update_count (CPUState *env, uint32_t count,
43                                    uint32_t compare)
44 {
45     uint64_t now, next;
46     uint32_t tmp;
47     
48     tmp = count;
49     if (count == compare)
50         tmp++;
51     now = qemu_get_clock(vm_clock);
52     next = now + muldiv64(compare - tmp, ticks_per_sec, 100 * 1000 * 1000);
53     if (next == now)
54         next++;
55 #if 1
56     if (logfile) {
57         fprintf(logfile, "%s: 0x%08llx %08x %08x => 0x%08llx\n",
58                 __func__, now, count, compare, next - now);
59     }
60 #endif
61     /* Store new count and compare registers */
62     env->CP0_Compare = compare;
63     env->CP0_Count =
64         count - (uint32_t)muldiv64(now, 100 * 1000 * 1000, ticks_per_sec);
65     /* Adjust timer */
66     qemu_mod_timer(env->timer, next);
67 }
68
69 void cpu_mips_store_count (CPUState *env, uint32_t value)
70 {
71     cpu_mips_update_count(env, value, env->CP0_Compare);
72 }
73
74 void cpu_mips_store_compare (CPUState *env, uint32_t value)
75 {
76     cpu_mips_update_count(env, cpu_mips_get_count(env), value);
77     cpu_single_env->CP0_Cause &= ~0x00008000;
78     cpu_reset_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
79 }
80
81 static void mips_timer_cb (void *opaque)
82 {
83     CPUState *env;
84
85     env = opaque;
86 #if 1
87     if (logfile) {
88         fprintf(logfile, "%s\n", __func__);
89     }
90 #endif
91     cpu_mips_update_count(env, cpu_mips_get_count(env), env->CP0_Compare);
92     cpu_single_env->CP0_Cause |= 0x00008000;
93     cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
94 }
95
96 void cpu_mips_clock_init (CPUState *env)
97 {
98     env->timer = qemu_new_timer(vm_clock, &mips_timer_cb, env);
99     env->CP0_Compare = 0;
100     cpu_mips_update_count(env, 1, 0);
101 }
102
103 static void io_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
104 {
105     if (logfile)
106         fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
107     cpu_outb(NULL, addr & 0xffff, value);
108 }
109
110 static uint32_t io_readb (void *opaque, target_phys_addr_t addr)
111 {
112     uint32_t ret = cpu_inb(NULL, addr & 0xffff);
113     if (logfile)
114         fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
115     return ret;
116 }
117
118 static void io_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
119 {
120     if (logfile)
121         fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
122 #ifdef TARGET_WORDS_BIGENDIAN
123     value = bswap16(value);
124 #endif
125     cpu_outw(NULL, addr & 0xffff, value);
126 }
127
128 static uint32_t io_readw (void *opaque, target_phys_addr_t addr)
129 {
130     uint32_t ret = cpu_inw(NULL, addr & 0xffff);
131 #ifdef TARGET_WORDS_BIGENDIAN
132     ret = bswap16(ret);
133 #endif
134     if (logfile)
135         fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
136     return ret;
137 }
138
139 static void io_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
140 {
141     if (logfile)
142         fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
143 #ifdef TARGET_WORDS_BIGENDIAN
144     value = bswap32(value);
145 #endif
146     cpu_outl(NULL, addr & 0xffff, value);
147 }
148
149 static uint32_t io_readl (void *opaque, target_phys_addr_t addr)
150 {
151     uint32_t ret = cpu_inl(NULL, addr & 0xffff);
152
153 #ifdef TARGET_WORDS_BIGENDIAN
154     ret = bswap32(ret);
155 #endif
156     if (logfile)
157         fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
158     return ret;
159 }
160
161 CPUWriteMemoryFunc *io_write[] = {
162     &io_writeb,
163     &io_writew,
164     &io_writel,
165 };
166
167 CPUReadMemoryFunc *io_read[] = {
168     &io_readb,
169     &io_readw,
170     &io_readl,
171 };
172
173 void mips_r4k_init (int ram_size, int vga_ram_size, int boot_device,
174                     DisplayState *ds, const char **fd_filename, int snapshot,
175                     const char *kernel_filename, const char *kernel_cmdline,
176                     const char *initrd_filename)
177 {
178     char buf[1024];
179     target_ulong kernel_base, kernel_size, initrd_base, initrd_size;
180     unsigned long bios_offset;
181     int io_memory;
182     int linux_boot;
183     int ret;
184
185     printf("%s: start\n", __func__);
186     linux_boot = (kernel_filename != NULL);
187     /* allocate RAM */
188     cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
189     bios_offset = ram_size + vga_ram_size;
190     snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
191     printf("%s: load BIOS '%s' size %d\n", __func__, buf, BIOS_SIZE);
192     ret = load_image(buf, phys_ram_base + bios_offset);
193     if (ret != BIOS_SIZE) {
194         fprintf(stderr, "qemu: could not load MIPS bios '%s'\n", buf);
195         exit(1);
196     }
197     cpu_register_physical_memory((uint32_t)(0x1fc00000),
198                                  BIOS_SIZE, bios_offset | IO_MEM_ROM);
199 #if 0
200     memcpy(phys_ram_base + 0x10000, phys_ram_base + bios_offset, BIOS_SIZE);
201     cpu_single_env->PC = 0x80010004;
202 #else
203     cpu_single_env->PC = 0xBFC00004;
204 #endif
205     if (linux_boot) {
206         kernel_base = KERNEL_LOAD_ADDR;
207         /* now we can load the kernel */
208         kernel_size = load_image(kernel_filename,
209                                 phys_ram_base + (kernel_base - 0x80000000));
210         if (kernel_size == (target_ulong) -1) {
211             fprintf(stderr, "qemu: could not load kernel '%s'\n", 
212                     kernel_filename);
213             exit(1);
214         }
215         /* load initrd */
216         if (initrd_filename) {
217             initrd_base = INITRD_LOAD_ADDR;
218             initrd_size = load_image(initrd_filename,
219                                      phys_ram_base + initrd_base);
220             if (initrd_size == (target_ulong) -1) {
221                 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 
222                         initrd_filename);
223                 exit(1);
224             }
225         } else {
226             initrd_base = 0;
227             initrd_size = 0;
228         }
229         cpu_single_env->PC = KERNEL_LOAD_ADDR;
230     } else {
231         kernel_base = 0;
232         kernel_size = 0;
233         initrd_base = 0;
234         initrd_size = 0;
235     }
236
237     /* Init internal devices */
238     cpu_mips_clock_init(cpu_single_env);
239     cpu_mips_irqctrl_init();
240
241     /* Register 64 KB of ISA IO space at 0x14000000 */
242     io_memory = cpu_register_io_memory(0, io_read, io_write, NULL);
243     cpu_register_physical_memory(0x14000000, 0x00010000, io_memory);
244     isa_mem_base = 0x10000000;
245
246     isa_pic = pic_init(pic_irq_request, cpu_single_env);
247     pit = pit_init(0x40, 0);
248     serial_init(0x3f8, 4, serial_hds[0]);
249     vga_initialize(NULL, ds, phys_ram_base + ram_size, ram_size, 
250                    vga_ram_size, 0, 0);
251
252     isa_ne2000_init(0x300, 9, &nd_table[0]);
253 }
254
255 QEMUMachine mips_machine = {
256     "mips",
257     "mips r4k platform",
258     mips_r4k_init,
259 };