malta_machdep.c revision 216318
1168054Sflz/*-
2168054Sflz * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
3168054Sflz * All rights reserved.
4168054Sflz *
5168064Sflz * Redistribution and use in source and binary forms, with or without
6168064Sflz * modification, are permitted provided that the following conditions
7168064Sflz * are met:
8168064Sflz * 1. Redistributions of source code must retain the above copyright
9168064Sflz *    notice, this list of conditions and the following disclaimer.
10168064Sflz * 2. Redistributions in binary form must reproduce the above copyright
11168064Sflz *    notice, this list of conditions and the following disclaimer in the
12168064Sflz *    documentation and/or other materials provided with the distribution.
13168064Sflz *
14168064Sflz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15168064Sflz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16168064Sflz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17168064Sflz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18168064Sflz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19168054Sflz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20168054Sflz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21168064Sflz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22168054Sflz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23168180Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24168151Sphk * SUCH DAMAGE.
25168153Simp *
26168151Sphk * $FreeBSD: head/sys/mips/malta/malta_machdep.c 216318 2010-12-09 07:01:03Z gonzo $
27168153Simp */
28168153Simp#include <sys/cdefs.h>
29168153Simp__FBSDID("$FreeBSD: head/sys/mips/malta/malta_machdep.c 216318 2010-12-09 07:01:03Z gonzo $");
30168153Simp
31168153Simp#include "opt_ddb.h"
32168180Srwatson
33168127Sdwmalone#include <sys/param.h>
34168180Srwatson#include <sys/conf.h>
35168153Simp#include <sys/kernel.h>
36168153Simp#include <sys/systm.h>
37168127Sdwmalone#include <sys/imgact.h>
38168054Sflz#include <sys/bio.h>
39168054Sflz#include <sys/buf.h>
40168054Sflz#include <sys/bus.h>
41168054Sflz#include <sys/cpu.h>
42168149Simp#include <sys/cons.h>
43168149Simp#include <sys/exec.h>
44168179Sgabor#include <sys/ucontext.h>
45168083Sjkim#include <sys/proc.h>
46168123Snetchild#include <sys/kdb.h>
47168080Smlaier#include <sys/ptrace.h>
48168131Sbmah#include <sys/reboot.h>
49168080Smlaier#include <sys/signalvar.h>
50168150Srwatson#include <sys/sysent.h>
51168149Simp#include <sys/sysproto.h>
52168150Srwatson#include <sys/user.h>
53168054Sflz
54168060Sdds#include <vm/vm.h>
55168108Srafan#include <vm/vm_object.h>
56168083Sjkim#include <vm/vm_page.h>
57168153Simp#include <vm/vm_pager.h>
58168080Smlaier
59168149Simp#include <machine/clock.h>
60168127Sdwmalone#include <machine/cpu.h>
61168180Srwatson#include <machine/cpuregs.h>
62168149Simp#include <machine/hwfunc.h>
63168127Sdwmalone#include <machine/md_var.h>
64168054Sflz#include <machine/pmap.h>
65168150Srwatson#include <machine/trap.h>
66168081Sle
67168154Sgshapiro#ifdef TICK_USE_YAMON_FREQ
68168127Sdwmalone#include <mips/malta/yamon.h>
69168144Simp#endif
70168080Smlaier
71168150Srwatson#ifdef TICK_USE_MALTA_RTC
72168153Simp#include <mips/mips4k/malta/maltareg.h>
73168083Sjkim#include <dev/mc146818/mc146818reg.h>
74168111Smatteo#include <isa/rtc.h>
75168149Simp#endif
76168151Sphk
77168149Simp#include <mips/malta/maltareg.h>
78168180Srwatson
79168150Srwatsonextern int	*edata;
80168081Sleextern int	*end;
81168180Srwatson
82168167Sphilipvoid	lcd_init(void);
83168142Srwatsonvoid	lcd_puts(char *);
84168143Smjacobvoid	malta_reset(void);
85168080Smlaier
86168149Simp/*
87168130Sdelphij * Offsets to MALTA LCD characters.
88168123Snetchild */
89168171Sphilipstatic int malta_lcd_offs[] = {
90168097Sle	MALTA_ASCIIPOS0,
91168149Simp	MALTA_ASCIIPOS1,
92168127Sdwmalone	MALTA_ASCIIPOS2,
93168111Smatteo	MALTA_ASCIIPOS3,
94168151Sphk	MALTA_ASCIIPOS4,
95168101Sandre	MALTA_ASCIIPOS5,
96168108Srafan	MALTA_ASCIIPOS6,
97168149Simp	MALTA_ASCIIPOS7
98168149Simp};
99168149Simp
100168140Srwatsonvoid
101168118Srseplatform_cpu_init()
102168140Srwatson{
103168080Smlaier	/* Nothing special */
104168149Simp}
105168081Sle
106168167Sphilip/*
107168149Simp * Put character to Malta LCD at given position.
108168115Ssimon */
109168150Srwatsonstatic void
110168149Simpmalta_lcd_putc(int pos, char c)
111168080Smlaier{
112168180Srwatson	void *addr;
113168141Swilko	char *ch;
114168180Srwatson
115168054Sflz	if (pos < 0 || pos > 7)
116168151Sphk		return;
117168151Sphk	addr = (void *)(MALTA_ASCII_BASE + malta_lcd_offs[pos]);
118168151Sphk	ch = (char *)MIPS_PHYS_TO_KSEG0(addr);
119168054Sflz	*ch = c;
120168064Sflz}
121168064Sflz
122168054Sflz/*
123168151Sphk * Print given string on LCD.
124168151Sphk */
125168151Sphkstatic void
126168151Sphkmalta_lcd_print(char *str)
127168151Sphk{
128168151Sphk	int i;
129168151Sphk
130168101Sandre	if (str == NULL)
131168101Sandre		return;
132168083Sjkim
133168083Sjkim	for (i = 0; *str != '\0'; i++, str++)
134168152Srwatson		malta_lcd_putc(i, *str);
135168152Srwatson}
136168152Srwatson
137168152Srwatsonvoid
138168152Srwatsonlcd_init(void)
139168152Srwatson{
140168152Srwatson	malta_lcd_print("FreeBSD_");
141168152Srwatson}
142168080Smlaier
143168080Smlaiervoid
144168080Smlaierlcd_puts(char *s)
145168080Smlaier{
146168152Srwatson	malta_lcd_print(s);
147168152Srwatson}
148168140Srwatson
149168140Srwatson#ifdef TICK_USE_MALTA_RTC
150168101Sandrestatic __inline uint8_t
151168115Ssimonrtcin(uint8_t addr)
152168101Sandre{
153168152Srwatson
154168152Srwatson	*((volatile uint8_t *)
155168108Srafan	    MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
156168108Srafan	return (*((volatile uint8_t *)
157168083Sjkim	    MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))));
158168147Sdes}
159168147Sdes
160168083Sjkimstatic __inline void
161168147Sdeswritertc(uint8_t addr, uint8_t val)
162168140Srwatson{
163168140Srwatson
164168127Sdwmalone	*((volatile uint8_t *)
165168127Sdwmalone	    MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
166168127Sdwmalone	*((volatile uint8_t *)
167168140Srwatson	    MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))) = val;
168168140Srwatson}
169168140Srwatson#endif
170168149Simp
171168149Simpstatic void
172168156Sbmahmips_init(void)
173168149Simp{
174168149Simp	int i;
175168149Simp
176168149Simp	for (i = 0; i < 10; i++) {
177168149Simp		phys_avail[i] = 0;
178168149Simp	}
179168149Simp
180168149Simp	/* phys_avail regions are in bytes */
181168149Simp	phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
182168149Simp	phys_avail[1] = ctob(realmem);
183168149Simp
184168149Simp	dump_avail[0] = phys_avail[0];
185168149Simp	dump_avail[1] = phys_avail[1] - phys_avail[0];
186168149Simp
187168149Simp	physmem = realmem;
188168149Simp
189168149Simp	init_param1();
190168149Simp	init_param2(physmem);
191168149Simp	mips_cpu_init();
192168149Simp	pmap_bootstrap();
193168080Smlaier	mips_proc0_init();
194168150Srwatson	mutex_init();
195168150Srwatson	kdb_init();
196168150Srwatson#ifdef KDB
197168150Srwatson	if (boothowto & RB_KDB)
198168150Srwatson		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
199168180Srwatson#endif
200168080Smlaier}
201168152Srwatson
202168150Srwatsonvoid
203168152Srwatsonplatform_halt(void)
204168150Srwatson{
205168144Simp
206168152Srwatson}
207168151Sphk
208168151Sphk
209168144Simpvoid
210168180Srwatsonplatform_identify(void)
211168180Srwatson{
212168081Sle
213168123Snetchild}
214168081Sle
215168143Smjacob/*
216168167Sphilip * Perform a board-level soft-reset.
217168143Smjacob * Note that this is not emulated by gxemul.
218168081Sle */
219168081Slevoid
220168152Srwatsonplatform_reset(void)
221168152Srwatson{
222168080Smlaier	char *c;
223168080Smlaier
224168080Smlaier	c = (char *)MIPS_PHYS_TO_KSEG0(MALTA_SOFTRES);
225168080Smlaier	*c = MALTA_GORESET;
226168180Srwatson}
227168180Srwatson
228168130Sdelphijvoid
229168130Sdelphijplatform_trap_enter(void)
230168123Snetchild{
231168123Snetchild
232168167Sphilip}
233168111Smatteo
234168167Sphilipvoid
235168111Smatteoplatform_trap_exit(void)
236168152Srwatson{
237168152Srwatson
238168152Srwatson}
239168152Srwatson
240168080Smlaierstatic uint64_t
241168080Smlaiermalta_cpu_freq(void)
242168152Srwatson{
243168152Srwatson	uint64_t platform_counter_freq = 0;
244168152Srwatson
245168152Srwatson#if defined(TICK_USE_YAMON_FREQ)
246168111Smatteo	/*
247168111Smatteo	 * If we are running on a board which uses YAMON firmware,
248168111Smatteo	 * then query CPU pipeline clock from the syscon object.
249168140Srwatson	 * If unsuccessful, use hard-coded default.
250168156Sbmah	 */
251168140Srwatson	platform_counter_freq = yamon_getcpufreq();
252168140Srwatson
253168140Srwatson#elif defined(TICK_USE_MALTA_RTC)
254168140Srwatson	/*
255168140Srwatson	 * If we are running on a board with the MC146818 RTC,
256168140Srwatson	 * use it to determine CPU pipeline clock frequency.
257168140Srwatson	 */
258168140Srwatson	u_int64_t counterval[2];
259168152Srwatson
260168140Srwatson	/* Set RTC to binary mode. */
261168180Srwatson	writertc(RTC_STATUSB, (rtcin(RTC_STATUSB) | RTCSB_BCD));
262168140Srwatson
263168140Srwatson	/* Busy-wait for falling edge of RTC update. */
264168101Sandre	while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
265168080Smlaier		;
266168150Srwatson	while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
267168080Smlaier		;
268168060Sdds	counterval[0] = mips_rd_count();
269168054Sflz
270168180Srwatson	/* Busy-wait for falling edge of RTC update. */
271168180Srwatson	while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
272168180Srwatson		;
273168180Srwatson	while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
274168127Sdwmalone		;
275168127Sdwmalone	counterval[1] = mips_rd_count();
276168127Sdwmalone
277168140Srwatson	platform_counter_freq = counterval[1] - counterval[0];
278168140Srwatson#endif
279168054Sflz
280	if (platform_counter_freq == 0)
281		platform_counter_freq = MIPS_DEFAULT_HZ;
282
283	return (platform_counter_freq);
284}
285
286void
287platform_start(__register_t a0, __register_t a1,  __register_t a2,
288    __register_t a3)
289{
290	vm_offset_t kernend;
291	uint64_t platform_counter_freq;
292	int argc = a0;
293	char **argv = (char **)a1;
294	char **envp = (char **)a2;
295	unsigned int memsize = a3;
296	int i;
297
298	/* clear the BSS and SBSS segments */
299	kernend = (vm_offset_t)&end;
300	memset(&edata, 0, kernend - (vm_offset_t)(&edata));
301
302	mips_postboot_fixup();
303
304	mips_pcpu0_init();
305	platform_counter_freq = malta_cpu_freq();
306	mips_timer_early_init(platform_counter_freq);
307
308	cninit();
309	printf("entry: platform_start()\n");
310
311	bootverbose = 1;
312	if (bootverbose) {
313		printf("cmd line: ");
314		for (i = 0; i < argc; i++)
315			printf("%s ", argv[i]);
316		printf("\n");
317
318		printf("envp:\n");
319		for (i = 0; envp[i]; i += 2)
320			printf("\t%s = %s\n", envp[i], envp[i+1]);
321
322		printf("memsize = %08x\n", memsize);
323	}
324
325	realmem = btoc(memsize);
326	mips_init();
327
328	mips_timer_init_params(platform_counter_freq, 0);
329}
330