blob: f3b146c22d783ae2eb8b2bab77499c38dfc16850 [file] [log] [blame]
Wolfgang Denkf93ae782006-10-24 14:31:24 +02001/*
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
Wolfgang Denkf93ae782006-10-24 14:31:24 +020020#include <asm/io.h>
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010021#include <asm/arch/clk.h>
22#include <asm/arch/memory-map.h>
23
24#if defined(CONFIG_USART0)
25# define USART_ID 0
26# define USART_BASE USART0_BASE
27#elif defined(CONFIG_USART1)
28# define USART_ID 1
29# define USART_BASE USART1_BASE
30#elif defined(CONFIG_USART2)
31# define USART_ID 2
32# define USART_BASE USART2_BASE
33#elif defined(CONFIG_USART3)
34# define USART_ID 3
35# define USART_BASE USART3_BASE
36#endif
Wolfgang Denkf93ae782006-10-24 14:31:24 +020037
38#include "atmel_usart.h"
39
40DECLARE_GLOBAL_DATA_PTR;
41
42void serial_setbrg(void)
43{
44 unsigned long divisor;
45 unsigned long usart_hz;
46
47 /*
48 * Master Clock
49 * Baud Rate = --------------
50 * 16 * CD
51 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010052 usart_hz = get_usart_clk_rate(USART_ID);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020053 divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010054 usart3_writel(BRGR, USART3_BF(CD, divisor));
Wolfgang Denkf93ae782006-10-24 14:31:24 +020055}
56
57int serial_init(void)
58{
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010059 usart3_writel(CR, USART3_BIT(RSTRX) | USART3_BIT(RSTTX));
Wolfgang Denkf93ae782006-10-24 14:31:24 +020060
61 serial_setbrg();
62
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010063 usart3_writel(CR, USART3_BIT(RXEN) | USART3_BIT(TXEN));
64 usart3_writel(MR, (USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
65 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
66 | USART3_BF(CHRL, USART3_CHRL_8)
67 | USART3_BF(PAR, USART3_PAR_NONE)
68 | USART3_BF(NBSTOP, USART3_NBSTOP_1)));
Wolfgang Denkf93ae782006-10-24 14:31:24 +020069
70 return 0;
71}
72
73void serial_putc(char c)
74{
75 if (c == '\n')
76 serial_putc('\r');
77
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010078 while (!(usart3_readl(CSR) & USART3_BIT(TXRDY))) ;
79 usart3_writel(THR, c);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020080}
81
82void serial_puts(const char *s)
83{
84 while (*s)
85 serial_putc(*s++);
86}
87
88int serial_getc(void)
89{
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010090 while (!(usart3_readl(CSR) & USART3_BIT(RXRDY))) ;
91 return usart3_readl(RHR);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020092}
93
94int serial_tstc(void)
95{
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010096 return (usart3_readl(CSR) & USART3_BIT(RXRDY)) != 0;
Wolfgang Denkf93ae782006-10-24 14:31:24 +020097}