Code cleanup. Update CHANGELOG.
[oweals/u-boot.git] / drivers / atmel_usart.c
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <common.h>
19
20 #ifdef CONFIG_ATMEL_USART
21 #include <asm/io.h>
22 #include <asm/arch/platform.h>
23
24 #include "atmel_usart.h"
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 void serial_setbrg(void)
29 {
30         unsigned long divisor;
31         unsigned long usart_hz;
32
33         /*
34          *              Master Clock
35          * Baud Rate = --------------
36          *                16 * CD
37          */
38         usart_hz = pm_get_clock_freq(gd->console_uart->resource[0].u.clock.id);
39         divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
40         usart3_writel(gd->console_uart, BRGR, USART3_BF(CD, divisor));
41 }
42
43 int serial_init(void)
44 {
45         usart3_writel(gd->console_uart, CR,
46                       USART3_BIT(RSTRX) | USART3_BIT(RSTTX));
47
48         serial_setbrg();
49
50         usart3_writel(gd->console_uart, CR,
51                       USART3_BIT(RXEN) | USART3_BIT(TXEN));
52         usart3_writel(gd->console_uart, MR,
53                       USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
54                       | USART3_BF(USCLKS, USART3_USCLKS_MCK)
55                       | USART3_BF(CHRL, USART3_CHRL_8)
56                       | USART3_BF(PAR, USART3_PAR_NONE)
57                       | USART3_BF(NBSTOP, USART3_NBSTOP_1));
58
59         return 0;
60 }
61
62 void serial_putc(char c)
63 {
64         if (c == '\n')
65                 serial_putc('\r');
66
67         while (!(usart3_readl(gd->console_uart, CSR) & USART3_BIT(TXRDY))) ;
68         usart3_writel(gd->console_uart, THR, c);
69 }
70
71 void serial_puts(const char *s)
72 {
73         while (*s)
74                 serial_putc(*s++);
75 }
76
77 int serial_getc(void)
78 {
79         while (!(usart3_readl(gd->console_uart, CSR) & USART3_BIT(RXRDY))) ;
80         return usart3_readl(gd->console_uart, RHR);
81 }
82
83 int serial_tstc(void)
84 {
85         return (usart3_readl(gd->console_uart, CSR) & USART3_BIT(RXRDY)) != 0;
86 }
87
88 #endif /* CONFIG_ATMEL_USART */