Root/usbboot/xburst_stage1/common.c

1/*
2 * device board
3 *
4 * Copyright 2009 (C) Qi Hardware Inc.,
5 * Author: Xiangfu Liu <xiangfu@sharism.cc>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 3 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA
20 */
21
22#include "target/jz4740.h"
23#include "target/configs.h"
24
25void serial_putc (const char c)
26{
27    volatile u8 *uart_lsr = (volatile u8 *)(UART_BASE + OFF_LSR);
28    volatile u8 *uart_tdr = (volatile u8 *)(UART_BASE + OFF_TDR);
29
30    if (c == '\n') serial_putc ('\r');
31
32    /* Wait for fifo to shift out some bytes */
33    while ( !((*uart_lsr & (UART_LSR_TDRQ | UART_LSR_TEMT)) == 0x60) );
34
35    *uart_tdr = (u8)c;
36}
37
38void serial_puts (const char *s)
39{
40    while (*s) {
41        serial_putc (*s++);
42    }
43}
44
45int serial_getc (void)
46{
47    volatile u8 *uart_rdr = (volatile u8 *)(UART_BASE + OFF_RDR);
48
49    while (!serial_tstc());
50
51    return *uart_rdr;
52}
53
54int serial_tstc (void)
55{
56    volatile u8 *uart_lsr = (volatile u8 *)(UART_BASE + OFF_LSR);
57
58    if (*uart_lsr & UART_LSR_DR) {
59        /* Data in rfifo */
60        return (1);
61    }
62    return 0;
63}
64
65void serial_init(void)
66{
67    volatile u8 *uart_fcr = (volatile u8 *)(UART_BASE + OFF_FCR);
68    volatile u8 *uart_lcr = (volatile u8 *)(UART_BASE + OFF_LCR);
69    volatile u8 *uart_ier = (volatile u8 *)(UART_BASE + OFF_IER);
70    volatile u8 *uart_sircr = (volatile u8 *)(UART_BASE + OFF_SIRCR);
71
72    /* Disable port interrupts while changing hardware */
73    *uart_ier = 0;
74
75    /* Disable UART unit function */
76    *uart_fcr = ~UART_FCR_UUE;
77
78    /* Set both receiver and transmitter in UART mode (not SIR) */
79    *uart_sircr = ~(SIRCR_RSIRE | SIRCR_TSIRE);
80
81    /* Set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
82    *uart_lcr = UART_LCR_WLEN_8 | UART_LCR_STOP_1;
83
84    /* Set baud rate */
85    if ( CPU_ID == 0x4740 )
86        serial_setbrg_4740();
87    else
88        serial_setbrg_4760();
89
90    /* Enable UART unit, enable and clear FIFO */
91    *uart_fcr = UART_FCR_UUE | UART_FCR_FE | UART_FCR_TFLS | UART_FCR_RFLS;
92}
93
94void serial_put_hex(unsigned int d)
95{
96    unsigned char c[12];
97    char i;
98    for(i = 0; i < 8;i++)
99    {
100        c[i] = (d >> ((7 - i) * 4)) & 0xf;
101        if(c[i] < 10)
102            c[i] += 0x30;
103        else
104            c[i] += (0x41 - 10);
105    }
106    c[8] = '\n';
107    c[9] = 0;
108    serial_puts(c);
109
110}
111

Archive Download this file



interactive