summaryrefslogtreecommitdiff
path: root/arch/x86/lib/bios_setup.c
blob: 6949b35069b9c8b2b5677903bff1d83473ede4e7 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * (C) Copyright 2002
 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/*
 * Partly based on msbios.c from rolo 1.6:
 *----------------------------------------------------------------------
 * (C) Copyright 2000
 * Sysgo Real-Time Solutions GmbH
 * Klein-Winternheim, Germany
 *----------------------------------------------------------------------
 */

#include <common.h>
#include <pci.h>
#include <asm/realmode.h>
#include <asm/io.h>

DECLARE_GLOBAL_DATA_PTR;

#define NUMVECTS	256

#define BIOS_DATA        ((char*)0x400)
#define BIOS_DATA_SIZE   256
#define BIOS_BASE        ((char*)0xf0000)
#define BIOS_CS          0xf000

extern ulong __bios_start;
extern ulong __bios_size;

/* these are defined in a 16bit segment and needs
 * to be accessed with the RELOC_16_xxxx() macros below
 */
extern u16 ram_in_64kb_chunks;
extern u16 bios_equipment;
extern u8  pci_last_bus;

extern void *rm_int00;
extern void *rm_int01;
extern void *rm_int02;
extern void *rm_int03;
extern void *rm_int04;
extern void *rm_int05;
extern void *rm_int06;
extern void *rm_int07;
extern void *rm_int08;
extern void *rm_int09;
extern void *rm_int0a;
extern void *rm_int0b;
extern void *rm_int0c;
extern void *rm_int0d;
extern void *rm_int0e;
extern void *rm_int0f;
extern void *rm_int10;
extern void *rm_int11;
extern void *rm_int12;
extern void *rm_int13;
extern void *rm_int14;
extern void *rm_int15;
extern void *rm_int16;
extern void *rm_int17;
extern void *rm_int18;
extern void *rm_int19;
extern void *rm_int1a;
extern void *rm_int1b;
extern void *rm_int1c;
extern void *rm_int1d;
extern void *rm_int1e;
extern void *rm_int1f;
extern void *rm_def_int;

extern void *realmode_reset;
extern void *realmode_pci_bios_call_entry;

static int set_jmp_vector(int entry_point, void *target)
{
	if (entry_point & ~0xffff) {
		return -1;
	}

	if (((u32)target-0xf0000) & ~0xffff) {
		return -1;
	}
	printf("set_jmp_vector: 0xf000:%04x -> %p\n",
	       entry_point, target);

	/* jmp opcode */
	writeb(0xea, 0xf0000 + entry_point);

	/* offset */
	writew(((u32)target-0xf0000), 0xf0000 + entry_point + 1);

	/* segment */
	writew(0xf000, 0xf0000 + entry_point + 3);

	return 0;
}


/*
 ************************************************************
 * Install an interrupt vector
 ************************************************************
 */

static void setvector(int vector, u16 segment, void *handler)
{
	u16 *ptr = (u16*)(vector*4);
	ptr[0] = ((u32)handler - (segment << 4))&0xffff;
	ptr[1] = segment;

#if 0
	printf("setvector: int%02x -> %04x:%04x\n",
	       vector, ptr[1], ptr[0]);
#endif
}

#define RELOC_16_LONG(seg, off) *(u32*)(seg << 4 | (u32)&off)
#define RELOC_16_WORD(seg, off) *(u16*)(seg << 4 | (u32)&off)
#define RELOC_16_BYTE(seg, off) *(u8*)(seg << 4 | (u32)&off)

int bios_setup(void)
{
	ulong bios_start = (ulong)&__bios_start + gd->reloc_off;
	ulong bios_size = (ulong)&__bios_size;

	static int done=0;
	int vector;
#ifdef CONFIG_PCI
	struct pci_controller *pri_hose;
#endif
	if (done) {
		return 0;
	}
	done = 1;

	if (bios_size > 65536) {
		printf("BIOS too large (%ld bytes, max is 65536)\n",
		       bios_size);
		return -1;
	}

	memcpy(BIOS_BASE, (void*)bios_start, bios_size);

	/* clear bda */
	memset(BIOS_DATA, 0, BIOS_DATA_SIZE);

	/* enter some values to the bda */
	writew(0x3f8, BIOS_DATA);   /* com1 addr */
	writew(0x2f8, BIOS_DATA+2); /* com2 addr */
	writew(0x3e8, BIOS_DATA+4); /* com3 addr */
	writew(0x2e8, BIOS_DATA+6); /* com4 addr */
	writew(0x278, BIOS_DATA+8); /* lpt1 addr */
	/*
	 * The kernel wants to read the base memory size
	 * from 40:13. Put a zero there to avoid an error message
	 */
	writew(0, BIOS_DATA+0x13);  /* base memory size */


	/* setup realmode interrupt vectors */
	for (vector = 0; vector < NUMVECTS; vector++) {
		setvector(vector, BIOS_CS, &rm_def_int);
	}

	setvector(0x00, BIOS_CS, &rm_int00);
	setvector(0x01, BIOS_CS, &rm_int01);
	setvector(0x02, BIOS_CS, &rm_int02);
	setvector(0x03, BIOS_CS, &rm_int03);
	setvector(0x04, BIOS_CS, &rm_int04);
	setvector(0x05, BIOS_CS, &rm_int05);
	setvector(0x06, BIOS_CS, &rm_int06);
	setvector(0x07, BIOS_CS, &rm_int07);
	setvector(0x08, BIOS_CS, &rm_int08);
	setvector(0x09, BIOS_CS, &rm_int09);
	setvector(0x0a, BIOS_CS, &rm_int0a);
	setvector(0x0b, BIOS_CS, &rm_int0b);
	setvector(0x0c, BIOS_CS, &rm_int0c);
	setvector(0x0d, BIOS_CS, &rm_int0d);
	setvector(0x0e, BIOS_CS, &rm_int0e);
	setvector(0x0f, BIOS_CS, &rm_int0f);
	setvector(0x10, BIOS_CS, &rm_int10);
	setvector(0x11, BIOS_CS, &rm_int11);
	setvector(0x12, BIOS_CS, &rm_int12);
	setvector(0x13, BIOS_CS, &rm_int13);
	setvector(0x14, BIOS_CS, &rm_int14);
	setvector(0x15, BIOS_CS, &rm_int15);
	setvector(0x16, BIOS_CS, &rm_int16);
	setvector(0x17, BIOS_CS, &rm_int17);
	setvector(0x18, BIOS_CS, &rm_int18);
	setvector(0x19, BIOS_CS, &rm_int19);
	setvector(0x1a, BIOS_CS, &rm_int1a);
	setvector(0x1b, BIOS_CS, &rm_int1b);
	setvector(0x1c, BIOS_CS, &rm_int1c);
	setvector(0x1d, BIOS_CS, &rm_int1d);
	setvector(0x1e, BIOS_CS, &rm_int1e);
	setvector(0x1f, BIOS_CS, &rm_int1f);

	set_jmp_vector(0xfff0, &realmode_reset);
	set_jmp_vector(0xfe6e, &realmode_pci_bios_call_entry);

	/* fill in data area */
	RELOC_16_WORD(0xf000, ram_in_64kb_chunks) = gd->ram_size >> 16;
	RELOC_16_WORD(0xf000, bios_equipment) = 0; /* FixMe */

	/* If we assume only one PCI hose, this PCI hose
	 * will own PCI bus #0, and the last PCI bus of
	 * that PCI hose will be the last PCI bus in the
	 * system.
	 * (This, ofcause break on multi hose systems,
	 *  but our PCI BIOS only support one hose anyway)
	 */
#ifdef CONFIG_PCI
	pri_hose = pci_bus_to_hose(0);
	if (NULL != pri_hose) {
		/* fill in last pci bus number for use by the realmode
		 * PCI BIOS */
		RELOC_16_BYTE(0xf000, pci_last_bus) = pri_hose->last_busno;
	}
#endif
	return 0;
}