xref: /netbsd-src/sys/arch/evbarm/stand/bootimx23/common.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $Id: common.c,v 1.4 2013/10/07 17:36:40 matt Exp $ */
2 
3 /*
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Petri Laakso.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/cdefs.h>
35 
36 #include <lib/libsa/stand.h>
37 
38 #include <arm/imx/imx23_digctlreg.h>
39 #include <arm/imx/imx23_uartdbgreg.h>
40 
41 #include "common.h"
42 
43 /*
44  * Delay us microseconds.
45  */
46 void
47 delay(unsigned int us)
48 {
49         volatile uint32_t *us_r;
50 
51 	us_r = (uint32_t *)(HW_DIGCTL_BASE + HW_DIGCTL_MICROSECONDS);
52 
53         *us_r = 0;
54         while (*us_r < us)
55                 ;
56 
57         return;
58 }
59 
60 /*
61  * Write character c to debug UART.
62  */
63 void
64 putchar(int c)
65 {
66 	volatile uint8_t *fr_r, *dr_r;
67 
68 	fr_r = (uint8_t *)(HW_UARTDBG_BASE + HW_UARTDBGFR);
69 	dr_r = (uint8_t *)(HW_UARTDBG_BASE + HW_UARTDBGDR);
70 
71         /* Wait until transmit FIFO has space for the new character. */
72         while (*fr_r & HW_UARTDBGFR_TXFF)
73                 ;
74 
75 	*dr_r = c;
76 #ifdef DIAGNOSTIC
77 
78 	/* Flush: Wait until transmit FIFO contents are written to UART. */
79 	while (!(*fr_r & HW_UARTDBGFR_TXFE))
80 		;
81 #endif
82 
83 	return;
84 }
85 
86 /*
87  * Read character from debug UART.
88  */
89 int
90 getchar(void)
91 {
92 	volatile uint8_t *fr_r, *dr_r;
93 
94 	fr_r = (uint8_t *)(HW_UARTDBG_BASE + HW_UARTDBGFR);
95 	dr_r = (uint8_t *)(HW_UARTDBG_BASE + HW_UARTDBGDR);
96 
97 	/* Wait until receive FIFO has character(s) */
98 	while (*fr_r & HW_UARTDBGFR_RXFE)
99 		;
100 
101 	return *dr_r;
102 }
103 
104 void
105 vpanic(const char *fmt, va_list ap)
106 {
107 	printf(fmt, ap);
108 	for(;;);
109 
110 	/* NOTREACHED */
111 }
112