]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/serial/stmp3xxx_dbguart.c
Unified codebase for TX28, TX48, TX51, TX53
[karo-tx-uboot.git] / drivers / serial / stmp3xxx_dbguart.c
1 /*
2  * (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>
3  *
4  * (C) Copyright 2009 Freescale Semiconductor, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #include <common.h>
22
23 #ifdef CONFIG_STMP3XXX_DBGUART
24
25 #include "stmp3xxx_dbguart.h"
26
27 DECLARE_GLOBAL_DATA_PTR;
28
29 /*
30  * Set baud rate. The settings are always 8n1:
31  * 8 data bits, no parity, 1 stop bit
32  */
33 void serial_setbrg(void)
34 {
35         u32 cr, lcr_h;
36         u32 quot;
37
38         /* Disable everything */
39         cr = REG_RD(DBGUART_BASE + UARTDBGCR);
40         REG_WR(DBGUART_BASE + UARTDBGCR, 0);
41
42         /* Calculate and set baudrate */
43         quot = (CONFIG_DBGUART_CLK * 4) / gd->baudrate;
44         REG_WR(DBGUART_BASE + UARTDBGFBRD, quot & 0x3f);
45         REG_WR(DBGUART_BASE + UARTDBGIBRD, quot >> 6);
46
47         /* Set 8n1 mode, enable FIFOs */
48         lcr_h = WLEN8 | FEN;
49         REG_WR(DBGUART_BASE + UARTDBGLCR_H, lcr_h);
50
51         /* Enable Debug UART */
52         REG_WR(DBGUART_BASE + UARTDBGCR, cr);
53 }
54
55 int serial_init(void)
56 {
57         u32 cr;
58
59         /* Disable UART */
60         REG_WR(DBGUART_BASE + UARTDBGCR, 0);
61
62         /* Mask interrupts */
63         REG_WR(DBGUART_BASE + UARTDBGIMSC, 0);
64
65         /* Set default baudrate */
66         serial_setbrg();
67
68         /* Enable UART */
69         cr = TXE | RXE | UARTEN;
70         REG_WR(DBGUART_BASE + UARTDBGCR, cr);
71
72         return 0;
73 }
74
75 /* Send a character */
76 void serial_putc(const char c)
77 {
78         /* Wait for room in TX FIFO */
79         while (REG_RD(DBGUART_BASE + UARTDBGFR) & TXFF)
80                 ;
81
82         /* Write the data byte */
83         REG_WR(DBGUART_BASE + UARTDBGDR, c);
84
85         if (c == '\n')
86                 serial_putc('\r');
87 }
88
89 void serial_puts(const char *s)
90 {
91         while (*s) {
92                 serial_putc(*s++);
93         }
94 }
95
96 /* Test whether a character is in TX buffer */
97 int serial_tstc(void)
98 {
99         /* Check if RX FIFO is not empty */
100         return !(REG_RD(DBGUART_BASE + UARTDBGFR) & RXFE);
101 }
102
103 /* Receive character */
104 int serial_getc(void)
105 {
106         /* Wait while TX FIFO is empty */
107         while (REG_RD(DBGUART_BASE + UARTDBGFR) & RXFE)
108                 ;
109
110         /* Read data byte */
111         return REG_RD(DBGUART_BASE + UARTDBGDR) & 0xff;
112 }
113
114 #endif /* CONFIG_STMP378X_DBGUART */