2 * Copyright (C) 2004-2006 Atmel Corporation
4 * Modified to support C structur SoC access by
5 * Andreas Bießmann <biessmann@corscience.de>
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.
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.
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
24 #include <linux/compiler.h>
27 #include <asm/arch/clk.h>
28 #include <asm/arch/hardware.h>
30 #include "atmel_usart.h"
32 DECLARE_GLOBAL_DATA_PTR;
34 static void atmel_serial_setbrg(void)
36 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
37 unsigned long divisor;
38 unsigned long usart_hz;
42 * Baud Rate = --------------
45 usart_hz = get_usart_clk_rate(CONFIG_USART_ID);
46 divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
47 writel(USART3_BF(CD, divisor), &usart->brgr);
50 static int atmel_serial_init(void)
52 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
55 * Just in case: drain transmitter register
56 * 1000us is enough for baudrate >= 9600
58 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
61 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
65 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
66 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
67 | USART3_BF(CHRL, USART3_CHRL_8)
68 | USART3_BF(PAR, USART3_PAR_NONE)
69 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
71 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
72 /* 100us is enough for the new settings to be settled */
78 static void atmel_serial_putc(char c)
80 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
85 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
86 writel(c, &usart->thr);
89 static void atmel_serial_puts(const char *s)
95 static int atmel_serial_getc(void)
97 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
99 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
101 return readl(&usart->rhr);
104 static int atmel_serial_tstc(void)
106 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
107 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
110 static struct serial_device atmel_serial_drv = {
111 .name = "atmel_serial",
112 .start = atmel_serial_init,
114 .setbrg = atmel_serial_setbrg,
115 .putc = atmel_serial_putc,
116 .puts = atmel_serial_puts,
117 .getc = atmel_serial_getc,
118 .tstc = atmel_serial_tstc,
121 void atmel_serial_initialize(void)
123 serial_register(&atmel_serial_drv);
126 __weak struct serial_device *default_serial_console(void)
128 return &atmel_serial_drv;