summaryrefslogtreecommitdiff
path: root/arch/v850/kernel/sim.c
blob: 4f31da96263277a6af51f58de67183cd4cb29ff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * arch/v850/kernel/sim.c -- Machine-specific stuff for GDB v850e simulator
 *
 *  Copyright (C) 2001,02  NEC Corporation
 *  Copyright (C) 2001,02  Miles Bader <miles@gnu.org>
 *
 * This file is subject to the terms and conditions of the GNU General
 * Public License.  See the file COPYING in the main directory of this
 * archive for more details.
 *
 * Written by Miles Bader <miles@gnu.org>
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/swap.h>
#include <linux/bootmem.h>
#include <linux/irq.h>

#include <asm/atomic.h>
#include <asm/page.h>
#include <asm/machdep.h>
#include <asm/simsyscall.h>

#include "mach.h"

/* The name of a file containing the root filesystem.  */
#define ROOT_FS "rootfs.image"

extern void simcons_setup (void);
extern void simcons_poll_ttys (void);
extern void set_mem_root (void *addr, size_t len, char *cmd_line);

static int read_file (const char *name,
		      unsigned long *addr, unsigned long *len,
		      const char **err);

void __init mach_setup (char **cmdline)
{
	const char *err;
	unsigned long root_dev_addr, root_dev_len;

	simcons_setup ();

	printk (KERN_INFO "Reading root filesystem: %s", ROOT_FS);

	if (read_file (ROOT_FS, &root_dev_addr, &root_dev_len, &err)) {
		printk (" (size %luK)\n", root_dev_len / 1024);
		set_mem_root ((void *)root_dev_addr, (size_t)root_dev_len,
			      *cmdline);
	} else
		printk ("...%s failed!\n", err);
}

void mach_get_physical_ram (unsigned long *ram_start, unsigned long *ram_len)
{
	*ram_start = RAM_ADDR;
	*ram_len = RAM_SIZE;
}

void __init mach_sched_init (struct irqaction *timer_action)
{
	/* ...do magic timer initialization?...  */
	mach_tick = simcons_poll_ttys;
	setup_irq (0, timer_action);
}


static void irq_nop (unsigned irq) { }
static unsigned irq_zero (unsigned irq) { return 0; }

static struct hw_interrupt_type sim_irq_type = {
	"IRQ",
	irq_zero,		/* startup */
	irq_nop,		/* shutdown */
	irq_nop,		/* enable */
	irq_nop,		/* disable */
	irq_nop,		/* ack */
	irq_nop,		/* end */
};

void __init mach_init_irqs (void)
{
	init_irq_handlers (0, NUM_MACH_IRQS, 1, &sim_irq_type);
}


void mach_gettimeofday (struct timespec *tv)
{
	long timeval[2], timezone[2];
	int rval = V850_SIM_SYSCALL (gettimeofday, timeval, timezone);
	if (rval == 0) {
		tv->tv_sec = timeval[0];
		tv->tv_nsec = timeval[1] * 1000;
	}
}

void machine_restart (char *__unused)
{
	V850_SIM_SYSCALL (write, 1, "RESTART\n", 8);
	V850_SIM_SYSCALL (exit, 0);
}

EXPORT_SYMBOL(machine_restart);

void machine_halt (void)
{
	V850_SIM_SYSCALL (write, 1, "HALT\n", 5);
	V850_SIM_SYSCALL (exit, 0);
}

EXPORT_SYMBOL(machine_halt);

void machine_power_off (void)
{
	V850_SIM_SYSCALL (write, 1, "POWER OFF\n", 10);
	V850_SIM_SYSCALL (exit, 0);
}

EXPORT_SYMBOL(machine_power_off);


/* Load data from a file called NAME into ram.  The address and length
   of the data image are returned in ADDR and LEN.  */
static int __init
read_file (const char *name,
	   unsigned long *addr, unsigned long *len,
	   const char **err)
{
	int rval, fd;
	unsigned long cur, left;
	/* Note this is not a normal stat buffer, it's an ad-hoc
	   structure defined by the simulator.  */
	unsigned long stat_buf[10];

	/* Stat the file to find out the length.  */
	rval = V850_SIM_SYSCALL (stat, name, stat_buf);
	if (rval < 0) {
		if (err) *err = "stat";
		return 0;
	}
	*len = stat_buf[4];

	/* Open the file; `0' is O_RDONLY.  */
	fd = V850_SIM_SYSCALL (open, name, 0);
	if (fd < 0) {
		if (err) *err = "open";
		return 0;
	}

	*addr = (unsigned long)alloc_bootmem(*len);
	if (! *addr) {
		V850_SIM_SYSCALL (close, fd);
		if (err) *err = "alloc_bootmem";
		return 0;
	}

	cur = *addr;
	left = *len;
	while (left > 0) {
		int chunk = V850_SIM_SYSCALL (read, fd, cur, left);
		if (chunk <= 0)
			break;
		cur += chunk;
		left -= chunk;
	}
	V850_SIM_SYSCALL (close, fd);
	if (left > 0) {
		/* Some read failed.  */
		free_bootmem (*addr, *len);
		if (err) *err = "read";
		return 0;
	}

	return 1;
}