xref: /openbmc/u-boot/drivers/serial/atmel_usart.c (revision bfc325ed)
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * Modified to support C structur SoC access by
5  * Andreas Bießmann <biessmann@corscience.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include <common.h>
22 #include <watchdog.h>
23 
24 #include <asm/io.h>
25 #include <asm/arch/clk.h>
26 #include <asm/arch/hardware.h>
27 
28 #include "atmel_usart.h"
29 
30 DECLARE_GLOBAL_DATA_PTR;
31 
32 void serial_setbrg(void)
33 {
34 	atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
35 	unsigned long divisor;
36 	unsigned long usart_hz;
37 
38 	/*
39 	 *              Master Clock
40 	 * Baud Rate = --------------
41 	 *                16 * CD
42 	 */
43 	usart_hz = get_usart_clk_rate(CONFIG_USART_ID);
44 	divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
45 	writel(USART3_BF(CD, divisor), &usart->brgr);
46 }
47 
48 int serial_init(void)
49 {
50 	atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
51 
52 	/*
53 	 * Just in case: drain transmitter register
54 	 * 1000us is enough for baudrate >= 9600
55 	 */
56 	if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
57 		__udelay(1000);
58 
59 	writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
60 
61 	serial_setbrg();
62 
63 	writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
64 			   | USART3_BF(USCLKS, USART3_USCLKS_MCK)
65 			   | USART3_BF(CHRL, USART3_CHRL_8)
66 			   | USART3_BF(PAR, USART3_PAR_NONE)
67 			   | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
68 			   &usart->mr);
69 	writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
70 	/* 100us is enough for the new settings to be settled */
71 	__udelay(100);
72 
73 	return 0;
74 }
75 
76 void serial_putc(char c)
77 {
78 	atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
79 
80 	if (c == '\n')
81 		serial_putc('\r');
82 
83 	while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
84 	writel(c, &usart->thr);
85 }
86 
87 void serial_puts(const char *s)
88 {
89 	while (*s)
90 		serial_putc(*s++);
91 }
92 
93 int serial_getc(void)
94 {
95 	atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
96 
97 	while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
98 		 WATCHDOG_RESET();
99 	return readl(&usart->rhr);
100 }
101 
102 int serial_tstc(void)
103 {
104 	atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
105 	return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
106 }
107