octeon_machdep.c revision 233331
1194140Simp/*-
2194140Simp * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
3194140Simp * All rights reserved.
4194140Simp *
5194140Simp * Redistribution and use in source and binary forms, with or without
6194140Simp * modification, are permitted provided that the following conditions
7194140Simp * are met:
8194140Simp * 1. Redistributions of source code must retain the above copyright
9194140Simp *    notice, this list of conditions and the following disclaimer.
10194140Simp * 2. Redistributions in binary form must reproduce the above copyright
11194140Simp *    notice, this list of conditions and the following disclaimer in the
12194140Simp *    documentation and/or other materials provided with the distribution.
13194140Simp *
14194140Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15194140Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16194140Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17194140Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18194140Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19194140Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20194140Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21194140Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22194140Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23194140Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24194140Simp * SUCH DAMAGE.
25194140Simp *
26194140Simp * $FreeBSD: head/sys/mips/cavium/octeon_machdep.c 233331 2012-03-23 00:01:09Z gonzo $
27194140Simp */
28194140Simp#include <sys/cdefs.h>
29194140Simp__FBSDID("$FreeBSD: head/sys/mips/cavium/octeon_machdep.c 233331 2012-03-23 00:01:09Z gonzo $");
30194140Simp
31194140Simp#include <sys/param.h>
32196262Simp#include <sys/conf.h>
33196262Simp#include <sys/kernel.h>
34194140Simp#include <sys/systm.h>
35196262Simp#include <sys/imgact.h>
36196262Simp#include <sys/bio.h>
37196262Simp#include <sys/buf.h>
38196262Simp#include <sys/bus.h>
39196262Simp#include <sys/cpu.h>
40196262Simp#include <sys/cons.h>
41196262Simp#include <sys/exec.h>
42196262Simp#include <sys/ucontext.h>
43196262Simp#include <sys/proc.h>
44196262Simp#include <sys/kdb.h>
45196262Simp#include <sys/ptrace.h>
46196262Simp#include <sys/reboot.h>
47196262Simp#include <sys/signalvar.h>
48196262Simp#include <sys/sysent.h>
49196262Simp#include <sys/sysproto.h>
50210311Sjmallett#include <sys/time.h>
51210311Sjmallett#include <sys/timetc.h>
52196262Simp#include <sys/user.h>
53196262Simp
54196262Simp#include <vm/vm.h>
55196262Simp#include <vm/vm_object.h>
56196262Simp#include <vm/vm_page.h>
57196262Simp#include <vm/vm_pager.h>
58196262Simp
59196262Simp#include <machine/atomic.h>
60196262Simp#include <machine/cache.h>
61196262Simp#include <machine/clock.h>
62196262Simp#include <machine/cpu.h>
63194140Simp#include <machine/cpuregs.h>
64194140Simp#include <machine/cpufunc.h>
65202063Simp#include <mips/cavium/octeon_pcmap_regs.h>
66196262Simp#include <machine/hwfunc.h>
67196262Simp#include <machine/intr_machdep.h>
68196262Simp#include <machine/locore.h>
69196262Simp#include <machine/md_var.h>
70194140Simp#include <machine/pcpu.h>
71196262Simp#include <machine/pte.h>
72196262Simp#include <machine/trap.h>
73196262Simp#include <machine/vmparam.h>
74194140Simp
75210311Sjmallett#include <contrib/octeon-sdk/cvmx.h>
76210311Sjmallett#include <contrib/octeon-sdk/cvmx-bootmem.h>
77210311Sjmallett#include <contrib/octeon-sdk/cvmx-interrupt.h>
78210311Sjmallett#include <contrib/octeon-sdk/cvmx-version.h>
79210311Sjmallett
80232812Sjmallett#include <mips/cavium/octeon_irq.h>
81232812Sjmallett
82194140Simp#if defined(__mips_n64)
83196262Simp#define MAX_APP_DESC_ADDR     0xffffffffafffffff
84194140Simp#else
85196262Simp#define MAX_APP_DESC_ADDR     0xafffffff
86194140Simp#endif
87194140Simp
88210311Sjmallett#define OCTEON_CLOCK_DEFAULT (500 * 1000 * 1000)
89210311Sjmallett
90210311Sjmallettstruct octeon_feature_description {
91210311Sjmallett	octeon_feature_t ofd_feature;
92210311Sjmallett	const char *ofd_string;
93210311Sjmallett};
94210311Sjmallett
95196262Simpextern int	*edata;
96196262Simpextern int	*end;
97217518Simpextern char cpu_model[];
98217518Simpextern char cpu_board[];
99194140Simp
100210311Sjmallettstatic const struct octeon_feature_description octeon_feature_descriptions[] = {
101210311Sjmallett	{ OCTEON_FEATURE_SAAD,			"SAAD" },
102210311Sjmallett	{ OCTEON_FEATURE_ZIP,			"ZIP" },
103210311Sjmallett	{ OCTEON_FEATURE_CRYPTO,		"CRYPTO" },
104216069Sjmallett	{ OCTEON_FEATURE_DORM_CRYPTO,		"DORM_CRYPTO" },
105210311Sjmallett	{ OCTEON_FEATURE_PCIE,			"PCIE" },
106216069Sjmallett	{ OCTEON_FEATURE_SRIO,			"SRIO" },
107210311Sjmallett	{ OCTEON_FEATURE_KEY_MEMORY,		"KEY_MEMORY" },
108210311Sjmallett	{ OCTEON_FEATURE_LED_CONTROLLER,	"LED_CONTROLLER" },
109210311Sjmallett	{ OCTEON_FEATURE_TRA,			"TRA" },
110210311Sjmallett	{ OCTEON_FEATURE_MGMT_PORT,		"MGMT_PORT" },
111210311Sjmallett	{ OCTEON_FEATURE_RAID,			"RAID" },
112210311Sjmallett	{ OCTEON_FEATURE_USB,			"USB" },
113210311Sjmallett	{ OCTEON_FEATURE_NO_WPTR,		"NO_WPTR" },
114210311Sjmallett	{ OCTEON_FEATURE_DFA,			"DFA" },
115210311Sjmallett	{ OCTEON_FEATURE_MDIO_CLAUSE_45,	"MDIO_CLAUSE_45" },
116216069Sjmallett	{ OCTEON_FEATURE_NPEI,			"NPEI" },
117232812Sjmallett	{ OCTEON_FEATURE_ILK,			"ILK" },
118232812Sjmallett	{ OCTEON_FEATURE_HFA,			"HFA" },
119232812Sjmallett	{ OCTEON_FEATURE_DFM,			"DFM" },
120232812Sjmallett	{ OCTEON_FEATURE_CIU2,			"CIU2" },
121232812Sjmallett	{ OCTEON_FEATURE_DICI_MODE,		"DICI_MODE" },
122232812Sjmallett	{ OCTEON_FEATURE_BIT_EXTRACTOR,		"BIT_EXTRACTOR" },
123232812Sjmallett	{ OCTEON_FEATURE_NAND,			"NAND" },
124232812Sjmallett	{ OCTEON_FEATURE_MMC,			"MMC" },
125232812Sjmallett	{ OCTEON_FEATURE_PKND,			"PKND" },
126232812Sjmallett	{ OCTEON_FEATURE_CN68XX_WQE,		"CN68XX_WQE" },
127210311Sjmallett	{ 0,					NULL }
128210311Sjmallett};
129210311Sjmallett
130201530Simpuint64_t ciu_get_en_reg_addr_new(int corenum, int intx, int enx, int ciu_ip);
131201530Simpvoid ciu_dump_interrutps_enabled(int core_num, int intx, int enx, int ciu_ip);
132201530Simp
133210311Sjmallettstatic uint64_t octeon_get_ticks(void);
134210311Sjmallettstatic unsigned octeon_get_timecount(struct timecounter *tc);
135210311Sjmallett
136200344Simpstatic void octeon_boot_params_init(register_t ptr);
137200344Simp
138210311Sjmallettstatic struct timecounter octeon_timecounter = {
139210311Sjmallett	octeon_get_timecount,	/* get_timecount */
140210311Sjmallett	0,			/* no poll_pps */
141210311Sjmallett	0xffffffffu,		/* octeon_mask */
142210311Sjmallett	0,			/* frequency */
143210311Sjmallett	"Octeon",		/* name */
144210311Sjmallett	900,			/* quality (adjusted in code) */
145210311Sjmallett};
146210311Sjmallett
147198669Srrsvoid
148198669Srrsplatform_cpu_init()
149198669Srrs{
150198669Srrs	/* Nothing special yet */
151198669Srrs}
152196262Simp
153194140Simp/*
154194140Simp * Perform a board-level soft-reset.
155194140Simp */
156196314Simpvoid
157196314Simpplatform_reset(void)
158194140Simp{
159210311Sjmallett	cvmx_write_csr(CVMX_CIU_SOFT_RST, 1);
160194140Simp}
161194140Simp
162201530Simpvoid
163201530Simpocteon_led_write_char(int char_position, char val)
164194140Simp{
165201530Simp	uint64_t ptr = (OCTEON_CHAR_LED_BASE_ADDR | 0xf8);
166194140Simp
167210311Sjmallett	if (octeon_is_simulation())
168201530Simp		return;
169194140Simp
170201530Simp	char_position &= 0x7;  /* only 8 chars */
171201530Simp	ptr += char_position;
172201530Simp	oct_write8_x8(ptr, val);
173194140Simp}
174194140Simp
175201530Simpvoid
176201530Simpocteon_led_write_char0(char val)
177194140Simp{
178201530Simp	uint64_t ptr = (OCTEON_CHAR_LED_BASE_ADDR | 0xf8);
179194140Simp
180210311Sjmallett	if (octeon_is_simulation())
181201530Simp		return;
182201530Simp	oct_write8_x8(ptr, val);
183194140Simp}
184194140Simp
185201530Simpvoid
186201530Simpocteon_led_write_hexchar(int char_position, char hexval)
187194140Simp{
188201530Simp	uint64_t ptr = (OCTEON_CHAR_LED_BASE_ADDR | 0xf8);
189201530Simp	char char1, char2;
190194140Simp
191210311Sjmallett	if (octeon_is_simulation())
192201530Simp		return;
193194140Simp
194201530Simp	char1 = (hexval >> 4) & 0x0f; char1 = (char1 < 10)?char1+'0':char1+'7';
195201530Simp	char2 = (hexval  & 0x0f); char2 = (char2 < 10)?char2+'0':char2+'7';
196201530Simp	char_position &= 0x7;  /* only 8 chars */
197201530Simp	if (char_position > 6)
198201530Simp		char_position = 6;
199201530Simp	ptr += char_position;
200201530Simp	oct_write8_x8(ptr, char1);
201201530Simp	ptr++;
202201530Simp	oct_write8_x8(ptr, char2);
203194140Simp}
204194140Simp
205201530Simpvoid
206201530Simpocteon_led_write_string(const char *str)
207194140Simp{
208201530Simp	uint64_t ptr = (OCTEON_CHAR_LED_BASE_ADDR | 0xf8);
209201530Simp	int i;
210194140Simp
211210311Sjmallett	if (octeon_is_simulation())
212201530Simp		return;
213194140Simp
214201530Simp	for (i=0; i<8; i++, ptr++) {
215201530Simp		if (str && *str)
216201530Simp			oct_write8_x8(ptr, *str++);
217201530Simp		else
218201530Simp			oct_write8_x8(ptr, ' ');
219210311Sjmallett		(void)cvmx_read_csr(CVMX_MIO_BOOT_BIST_STAT);
220201530Simp	}
221194140Simp}
222194140Simp
223194140Simpstatic char progress[8] = { '-', '/', '|', '\\', '-', '/', '|', '\\'};
224194140Simp
225201530Simpvoid
226201530Simpocteon_led_run_wheel(int *prog_count, int led_position)
227194140Simp{
228210311Sjmallett	if (octeon_is_simulation())
229201530Simp		return;
230201530Simp	octeon_led_write_char(led_position, progress[*prog_count]);
231201530Simp	*prog_count += 1;
232201530Simp	*prog_count &= 0x7;
233194140Simp}
234194140Simp
235201530Simpvoid
236201530Simpocteon_led_write_hex(uint32_t wl)
237194140Simp{
238201530Simp	char nbuf[80];
239194140Simp
240201530Simp	sprintf(nbuf, "%X", wl);
241201530Simp	octeon_led_write_string(nbuf);
242194140Simp}
243194140Simp
244194140Simp/*
245194140Simp * octeon_debug_symbol
246194140Simp *
247194140Simp * Does nothing.
248194140Simp * Used to mark the point for simulator to begin tracing
249194140Simp */
250201530Simpvoid
251201530Simpocteon_debug_symbol(void)
252194140Simp{
253194140Simp}
254194140Simp
255194140Simp/*
256194140Simp * octeon_ciu_reset
257194140Simp *
258194140Simp * Shutdown all CIU to IP2, IP3 mappings
259194140Simp */
260201530Simpvoid
261201530Simpocteon_ciu_reset(void)
262194140Simp{
263210311Sjmallett	/* Disable all CIU interrupts by default */
264210311Sjmallett	cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num()*2), 0);
265210311Sjmallett	cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num()*2+1), 0);
266210311Sjmallett	cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num()*2), 0);
267210311Sjmallett	cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num()*2+1), 0);
268194140Simp
269210311Sjmallett#ifdef SMP
270210311Sjmallett	/* Enable the MBOX interrupts.  */
271210311Sjmallett	cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num()*2+1),
272232812Sjmallett		       (1ull << (OCTEON_IRQ_MBOX0 - 8)) |
273232812Sjmallett		       (1ull << (OCTEON_IRQ_MBOX1 - 8)));
274210311Sjmallett#endif
275194140Simp}
276194140Simp
277210311Sjmallettstatic void
278210311Sjmallettocteon_memory_init(void)
279194140Simp{
280210311Sjmallett	vm_paddr_t phys_end;
281210311Sjmallett	int64_t addr;
282216318Sgonzo	unsigned i, j;
283194140Simp
284210311Sjmallett	phys_end = round_page(MIPS_KSEG0_TO_PHYS((vm_offset_t)&end));
285194140Simp
286210311Sjmallett	if (octeon_is_simulation()) {
287210311Sjmallett		/* Simulator we limit to 96 meg */
288210311Sjmallett		phys_avail[0] = phys_end;
289210311Sjmallett		phys_avail[1] = 96 << 20;
290194140Simp
291216318Sgonzo		dump_avail[0] = phys_avail[0];
292216320Sgonzo		dump_avail[1] = phys_avail[1];
293216318Sgonzo
294210311Sjmallett		realmem = physmem = btoc(phys_avail[1] - phys_avail[0]);
295210311Sjmallett		return;
296201530Simp	}
297194140Simp
298210311Sjmallett	/*
299210311Sjmallett	 * Allocate memory from bootmem 1MB at a time and merge
300210311Sjmallett	 * adjacent entries.
301210311Sjmallett	 */
302210311Sjmallett	i = 0;
303210311Sjmallett	while (i < PHYS_AVAIL_ENTRIES) {
304216773Sjmallett		/*
305216773Sjmallett		 * If there is less than 2MB of memory available in 128-byte
306216773Sjmallett		 * blocks, do not steal any more memory.  We need to leave some
307216773Sjmallett		 * memory for the command queues to be allocated out of.
308216773Sjmallett		 */
309216773Sjmallett		if (cvmx_bootmem_available_mem(128) < 2 << 20)
310216773Sjmallett			break;
311216773Sjmallett
312210311Sjmallett		addr = cvmx_bootmem_phy_alloc(1 << 20, phys_end,
313210311Sjmallett					      ~(vm_paddr_t)0, PAGE_SIZE, 0);
314210311Sjmallett		if (addr == -1)
315210311Sjmallett			break;
316194140Simp
317212842Sjmallett		/*
318212842Sjmallett		 * The SDK needs to be able to easily map any memory that might
319212842Sjmallett		 * come to it e.g. in the form of an mbuf.  Because on !n64 we
320212842Sjmallett		 * can't direct-map some addresses and we don't want to manage
321212842Sjmallett		 * temporary mappings within the SDK, don't feed memory that
322212842Sjmallett		 * can't be direct-mapped to the kernel.
323212842Sjmallett		 */
324212842Sjmallett#if !defined(__mips_n64)
325212842Sjmallett		if (!MIPS_DIRECT_MAPPABLE(addr + (1 << 20) - 1))
326212842Sjmallett			continue;
327212842Sjmallett#endif
328212842Sjmallett
329210311Sjmallett		physmem += btoc(1 << 20);
330194140Simp
331210311Sjmallett		if (i > 0 && phys_avail[i - 1] == addr) {
332210311Sjmallett			phys_avail[i - 1] += 1 << 20;
333210311Sjmallett			continue;
334210311Sjmallett		}
335194140Simp
336210311Sjmallett		phys_avail[i + 0] = addr;
337210311Sjmallett		phys_avail[i + 1] = addr + (1 << 20);
338194140Simp
339210311Sjmallett		i += 2;
340201530Simp	}
341194140Simp
342216320Sgonzo	for (j = 0; j < i; j++)
343216318Sgonzo		dump_avail[j] = phys_avail[j];
344216318Sgonzo
345202831Simp	realmem = physmem;
346202831Simp}
347202831Simp
348196262Simpvoid
349201530Simpplatform_start(__register_t a0, __register_t a1, __register_t a2 __unused,
350201530Simp    __register_t a3)
351196262Simp{
352210311Sjmallett	const struct octeon_feature_description *ofd;
353196262Simp	uint64_t platform_counter_freq;
354194140Simp
355210311Sjmallett	/*
356210311Sjmallett	 * XXX
357210311Sjmallett	 * octeon_boot_params_init() should be called before anything else,
358210311Sjmallett	 * certainly before any output; we may find out from the boot
359210311Sjmallett	 * descriptor's flags that we're supposed to use the PCI or UART1
360210311Sjmallett	 * consoles rather than UART0.  No point doing that reorganization
361210311Sjmallett	 * until we actually intercept UART_DEV_CONSOLE for the UART1 case
362210311Sjmallett	 * and somehow handle the PCI console, which we lack code for
363210311Sjmallett	 * entirely.
364210311Sjmallett	 */
365210311Sjmallett
366233331Sgonzo	mips_postboot_fixup();
367233331Sgonzo
368201845Simp	/* Initialize pcpu stuff */
369201881Simp	mips_pcpu0_init();
370202831Simp	mips_timer_early_init(OCTEON_CLOCK_DEFAULT);
371202831Simp	cninit();
372201845Simp
373202831Simp	octeon_ciu_reset();
374200344Simp	octeon_boot_params_init(a3);
375210311Sjmallett	/*
376210311Sjmallett	 * XXX
377210311Sjmallett	 * We can certainly parse command line arguments or U-Boot environment
378210311Sjmallett	 * to determine whether to bootverbose / single user / ...  I think
379210311Sjmallett	 * stass has patches to add support for loader things to U-Boot even.
380210311Sjmallett	 */
381196262Simp	bootverbose = 1;
382194140Simp
383202831Simp	/*
384202831Simp	 * For some reason on the cn38xx simulator ebase register is set to
385202831Simp	 * 0x80001000 at bootup time.  Move it back to the default, but
386202831Simp	 * when we move to having support for multiple executives, we need
387202831Simp	 * to rethink this.
388202831Simp	 */
389202831Simp	mips_wr_ebase(0x80000000);
390196262Simp
391202831Simp	octeon_memory_init();
392196262Simp	init_param1();
393196262Simp	init_param2(physmem);
394196262Simp	mips_cpu_init();
395202831Simp	pmap_bootstrap();
396202831Simp	mips_proc0_init();
397196262Simp	mutex_init();
398196262Simp	kdb_init();
399202850Simp#ifdef KDB
400202850Simp	if (boothowto & RB_KDB)
401202850Simp		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
402196262Simp#endif
403217518Simp	cpu_clock = cvmx_sysinfo_get()->cpu_clock_hz;
404217518Simp	platform_counter_freq = cpu_clock;
405217518Simp	octeon_timecounter.tc_frequency = cpu_clock;
406210311Sjmallett	platform_timecounter = &octeon_timecounter;
407206721Sjmallett	mips_timer_init_params(platform_counter_freq, 0);
408217518Simp	set_cputicker(octeon_get_ticks, cpu_clock, 0);
409206721Sjmallett
410206721Sjmallett#ifdef SMP
411206721Sjmallett	/*
412210311Sjmallett	 * Clear any pending IPIs.
413206721Sjmallett	 */
414210311Sjmallett	cvmx_write_csr(CVMX_CIU_MBOX_CLRX(0), 0xffffffff);
415206721Sjmallett#endif
416210311Sjmallett
417210311Sjmallett	printf("Octeon SDK: %s\n", OCTEON_SDK_VERSION_STRING);
418210311Sjmallett	printf("Available Octeon features:");
419210311Sjmallett	for (ofd = octeon_feature_descriptions; ofd->ofd_string != NULL; ofd++)
420210311Sjmallett		if (octeon_has_feature(ofd->ofd_feature))
421210311Sjmallett			printf(" %s", ofd->ofd_string);
422210311Sjmallett	printf("\n");
423196262Simp}
424196262Simp
425210311Sjmallettstatic uint64_t
426210311Sjmallettocteon_get_ticks(void)
427210311Sjmallett{
428210311Sjmallett	uint64_t cvmcount;
429210311Sjmallett
430210311Sjmallett	CVMX_MF_CYCLE(cvmcount);
431210311Sjmallett	return (cvmcount);
432210311Sjmallett}
433210311Sjmallett
434210311Sjmallettstatic unsigned
435210311Sjmallettocteon_get_timecount(struct timecounter *tc)
436210311Sjmallett{
437210311Sjmallett	return ((unsigned)octeon_get_ticks());
438210311Sjmallett}
439210311Sjmallett
440215990Sjmallett/**
441215990Sjmallett * version of printf that works better in exception context.
442215990Sjmallett *
443215990Sjmallett * @param format
444215990Sjmallett *
445215990Sjmallett * XXX If this function weren't in cvmx-interrupt.c, we'd use the SDK version.
446215990Sjmallett */
447215990Sjmallettvoid cvmx_safe_printf(const char *format, ...)
448215990Sjmallett{
449215990Sjmallett    char buffer[256];
450215990Sjmallett    char *ptr = buffer;
451215990Sjmallett    int count;
452215990Sjmallett    va_list args;
453215990Sjmallett
454215990Sjmallett    va_start(args, format);
455215990Sjmallett#ifndef __U_BOOT__
456215990Sjmallett    count = vsnprintf(buffer, sizeof(buffer), format, args);
457215990Sjmallett#else
458215990Sjmallett    count = vsprintf(buffer, format, args);
459215990Sjmallett#endif
460215990Sjmallett    va_end(args);
461215990Sjmallett
462215990Sjmallett    while (count-- > 0)
463215990Sjmallett    {
464215990Sjmallett        cvmx_uart_lsr_t lsrval;
465215990Sjmallett
466215990Sjmallett        /* Spin until there is room */
467215990Sjmallett        do
468215990Sjmallett        {
469215990Sjmallett            lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(0));
470215990Sjmallett#if !defined(CONFIG_OCTEON_SIM_SPEED)
471215990Sjmallett            if (lsrval.s.temt == 0)
472215990Sjmallett                cvmx_wait(10000);   /* Just to reduce the load on the system */
473215990Sjmallett#endif
474215990Sjmallett        }
475215990Sjmallett        while (lsrval.s.temt == 0);
476215990Sjmallett
477215990Sjmallett        if (*ptr == '\n')
478215990Sjmallett            cvmx_write_csr(CVMX_MIO_UARTX_THR(0), '\r');
479215990Sjmallett        cvmx_write_csr(CVMX_MIO_UARTX_THR(0), *ptr++);
480215990Sjmallett    }
481215990Sjmallett}
482215990Sjmallett
483202831Simp/* impSTART: This stuff should move back into the Cavium SDK */
484194140Simp/*
485194140Simp ****************************************************************************************
486194140Simp *
487194140Simp * APP/BOOT  DESCRIPTOR  STUFF
488194140Simp *
489194140Simp ****************************************************************************************
490194140Simp */
491194140Simp
492194140Simp/* Define the struct that is initialized by the bootloader used by the
493194140Simp * startup code.
494194140Simp *
495194140Simp * Copyright (c) 2004, 2005, 2006 Cavium Networks.
496194140Simp *
497194140Simp * The authors hereby grant permission to use, copy, modify, distribute,
498194140Simp * and license this software and its documentation for any purpose, provided
499194140Simp * that existing copyright notices are retained in all copies and that this
500194140Simp * notice is included verbatim in any distributions. No written agreement,
501194140Simp * license, or royalty fee is required for any of the authorized uses.
502194140Simp * Modifications to this software may be copyrighted by their authors
503194140Simp * and need not follow the licensing terms described here, provided that
504194140Simp * the new terms are clearly indicated on the first page of each file where
505194140Simp * they apply.
506194140Simp */
507194140Simp
508194140Simp#define OCTEON_CURRENT_DESC_VERSION     6
509194140Simp#define OCTEON_ARGV_MAX_ARGS            (64)
510194140Simp#define OCTOEN_SERIAL_LEN 20
511194140Simp
512194140Simptypedef struct {
513200344Simp	/* Start of block referenced by assembly code - do not change! */
514200344Simp	uint32_t desc_version;
515200344Simp	uint32_t desc_size;
516194140Simp
517200344Simp	uint64_t stack_top;
518200344Simp	uint64_t heap_base;
519200344Simp	uint64_t heap_end;
520200344Simp	uint64_t entry_point;   /* Only used by bootloader */
521200344Simp	uint64_t desc_vaddr;
522200344Simp	/* End of This block referenced by assembly code - do not change! */
523194140Simp
524200344Simp	uint32_t exception_base_addr;
525200344Simp	uint32_t stack_size;
526200344Simp	uint32_t heap_size;
527200344Simp	uint32_t argc;  /* Argc count for application */
528200344Simp	uint32_t argv[OCTEON_ARGV_MAX_ARGS];
529200344Simp	uint32_t flags;
530200344Simp	uint32_t core_mask;
531200344Simp	uint32_t dram_size;  /**< DRAM size in megabyes */
532200344Simp	uint32_t phy_mem_desc_addr;  /**< physical address of free memory descriptor block*/
533200344Simp	uint32_t debugger_flags_base_addr;  /**< used to pass flags from app to debugger */
534200344Simp	uint32_t eclock_hz;  /**< CPU clock speed, in hz */
535200344Simp	uint32_t dclock_hz;  /**< DRAM clock speed, in hz */
536200344Simp	uint32_t spi_clock_hz;  /**< SPI4 clock in hz */
537200344Simp	uint16_t board_type;
538200344Simp	uint8_t board_rev_major;
539200344Simp	uint8_t board_rev_minor;
540200344Simp	uint16_t chip_type;
541200344Simp	uint8_t chip_rev_major;
542200344Simp	uint8_t chip_rev_minor;
543200344Simp	char board_serial_number[OCTOEN_SERIAL_LEN];
544200344Simp	uint8_t mac_addr_base[6];
545200344Simp	uint8_t mac_addr_count;
546200344Simp	uint64_t cvmx_desc_vaddr;
547194140Simp} octeon_boot_descriptor_t;
548194140Simp
549210311Sjmallettcvmx_bootinfo_t *octeon_bootinfo;
550194140Simp
551194140Simpstatic octeon_boot_descriptor_t *app_desc_ptr;
552194140Simp
553200344Simpint
554210311Sjmallettocteon_is_simulation(void)
555194140Simp{
556210311Sjmallett	switch (cvmx_sysinfo_get()->board_type) {
557210311Sjmallett	case CVMX_BOARD_TYPE_SIM:
558204777Sjmallett		return 1;
559204777Sjmallett	default:
560210311Sjmallett		return 0;
561204777Sjmallett	}
562194140Simp}
563194140Simp
564200344Simpstatic void
565210311Sjmallettocteon_process_app_desc_ver_6(void)
566194140Simp{
567200344Simp	/* XXX Why is 0x00000000ffffffffULL a bad value?  */
568200344Simp	if (app_desc_ptr->cvmx_desc_vaddr == 0 ||
569210311Sjmallett	    app_desc_ptr->cvmx_desc_vaddr == 0xfffffffful)
570210311Sjmallett            	panic("Bad octeon_bootinfo %p", octeon_bootinfo);
571210311Sjmallett
572210311Sjmallett    	octeon_bootinfo =
573200344Simp	    (cvmx_bootinfo_t *)(intptr_t)app_desc_ptr->cvmx_desc_vaddr;
574210311Sjmallett        octeon_bootinfo =
575210311Sjmallett	    (cvmx_bootinfo_t *) ((intptr_t)octeon_bootinfo | MIPS_KSEG0_START);
576210311Sjmallett        if (octeon_bootinfo->major_version != 1)
577210311Sjmallett            	panic("Incompatible CVMX descriptor from bootloader: %d.%d %p",
578210311Sjmallett                       (int) octeon_bootinfo->major_version,
579210311Sjmallett                       (int) octeon_bootinfo->minor_version, octeon_bootinfo);
580194140Simp
581215990Sjmallett	cvmx_sysinfo_minimal_initialize(octeon_bootinfo->phy_mem_desc_addr,
582210311Sjmallett					octeon_bootinfo->board_type,
583210311Sjmallett					octeon_bootinfo->board_rev_major,
584210311Sjmallett					octeon_bootinfo->board_rev_minor,
585210311Sjmallett					octeon_bootinfo->eclock_hz);
586228088Sgonzo	memcpy(cvmx_sysinfo_get()->mac_addr_base, octeon_bootinfo->mac_addr_base, 6);
587228088Sgonzo	cvmx_sysinfo_get()->mac_addr_count = octeon_bootinfo->mac_addr_count;
588228853Sgonzo	cvmx_sysinfo_get()->compact_flash_common_base_addr =
589228853Sgonzo		octeon_bootinfo->compact_flash_common_base_addr;
590228872Sgonzo	cvmx_sysinfo_get()->compact_flash_attribute_base_addr =
591228872Sgonzo		octeon_bootinfo->compact_flash_attribute_base_addr;
592232402Sjmallett	cvmx_sysinfo_get()->core_mask = octeon_bootinfo->core_mask;
593194140Simp}
594194140Simp
595200344Simpstatic void
596200344Simpocteon_boot_params_init(register_t ptr)
597194140Simp{
598210311Sjmallett	if (ptr == 0 || ptr >= MAX_APP_DESC_ADDR)
599210311Sjmallett		panic("app descriptor passed at invalid address %#jx",
600210311Sjmallett		    (uintmax_t)ptr);
601194140Simp
602210311Sjmallett	app_desc_ptr = (octeon_boot_descriptor_t *)(intptr_t)ptr;
603210311Sjmallett	if (app_desc_ptr->desc_version < 6)
604210311Sjmallett		panic("Your boot code is too old to be supported.");
605210311Sjmallett	octeon_process_app_desc_ver_6();
606210311Sjmallett
607210311Sjmallett	KASSERT(octeon_bootinfo != NULL, ("octeon_bootinfo should be set"));
608210311Sjmallett
609215990Sjmallett	if (cvmx_sysinfo_get()->phy_mem_desc_addr == (uint64_t)0)
610210311Sjmallett		panic("Your boot loader did not supply a memory descriptor.");
611215990Sjmallett	cvmx_bootmem_init(cvmx_sysinfo_get()->phy_mem_desc_addr);
612210311Sjmallett
613194140Simp        printf("Boot Descriptor Ver: %u -> %u/%u",
614210311Sjmallett               app_desc_ptr->desc_version, octeon_bootinfo->major_version,
615210311Sjmallett	       octeon_bootinfo->minor_version);
616210311Sjmallett        printf("  CPU clock: %uMHz  Core Mask: %#x\n",
617210311Sjmallett	       cvmx_sysinfo_get()->cpu_clock_hz / 1000000,
618210311Sjmallett	       cvmx_sysinfo_get()->core_mask);
619194140Simp        printf("  Board Type: %u  Revision: %u/%u\n",
620210311Sjmallett               cvmx_sysinfo_get()->board_type,
621210311Sjmallett	       cvmx_sysinfo_get()->board_rev_major,
622210311Sjmallett	       cvmx_sysinfo_get()->board_rev_minor);
623194140Simp
624202831Simp        printf("  Mac Address %02X.%02X.%02X.%02X.%02X.%02X (%d)\n",
625210311Sjmallett	    octeon_bootinfo->mac_addr_base[0],
626210311Sjmallett	    octeon_bootinfo->mac_addr_base[1],
627210311Sjmallett	    octeon_bootinfo->mac_addr_base[2],
628210311Sjmallett	    octeon_bootinfo->mac_addr_base[3],
629210311Sjmallett	    octeon_bootinfo->mac_addr_base[4],
630210311Sjmallett	    octeon_bootinfo->mac_addr_base[5],
631210311Sjmallett	    octeon_bootinfo->mac_addr_count);
632210311Sjmallett
633210311Sjmallett#if defined(OCTEON_BOARD_CAPK_0100ND)
634217518Simp	strcpy(cpu_board, "CAPK-0100ND");
635217518Simp	if (cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_CN3010_EVB_HS5) {
636210311Sjmallett		printf("Compiled for CAPK-0100ND, but board type is %s\n",
637210311Sjmallett		    cvmx_board_type_to_string(cvmx_sysinfo_get()->board_type));
638217518Simp		strcat(cpu_board, " hardwired, but type is ");
639217518Simp		strcat(cpu_board,
640217518Simp		    cvmx_board_type_to_string(cvmx_sysinfo_get()->board_type));
641217518Simp	}
642210311Sjmallett#else
643217518Simp	strcpy(cpu_board,
644210311Sjmallett	    cvmx_board_type_to_string(cvmx_sysinfo_get()->board_type));
645217518Simp	printf("Board: %s\n", cpu_board);
646210311Sjmallett#endif
647217518Simp	strcpy(cpu_model, octeon_model_get_string(cvmx_get_proc_id()));
648217518Simp	printf("Model: %s\n", cpu_model);
649194140Simp}
650202831Simp/* impEND: This stuff should move back into the Cavium SDK */
651