Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 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> |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 22 | #include <asm/arch/clk.h> |
| 23 | #include <asm/arch/memory-map.h> |
| 24 | |
| 25 | #if defined(CONFIG_USART0) |
| 26 | # define USART_ID 0 |
| 27 | # define USART_BASE USART0_BASE |
| 28 | #elif defined(CONFIG_USART1) |
| 29 | # define USART_ID 1 |
| 30 | # define USART_BASE USART1_BASE |
| 31 | #elif defined(CONFIG_USART2) |
| 32 | # define USART_ID 2 |
| 33 | # define USART_BASE USART2_BASE |
| 34 | #elif defined(CONFIG_USART3) |
| 35 | # define USART_ID 3 |
| 36 | # define USART_BASE USART3_BASE |
| 37 | #endif |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 38 | |
| 39 | #include "atmel_usart.h" |
| 40 | |
| 41 | DECLARE_GLOBAL_DATA_PTR; |
| 42 | |
| 43 | void serial_setbrg(void) |
| 44 | { |
| 45 | unsigned long divisor; |
| 46 | unsigned long usart_hz; |
| 47 | |
| 48 | /* |
| 49 | * Master Clock |
| 50 | * Baud Rate = -------------- |
| 51 | * 16 * CD |
| 52 | */ |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 53 | usart_hz = get_usart_clk_rate(USART_ID); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 54 | divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate; |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 55 | usart3_writel(BRGR, USART3_BF(CD, divisor)); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | int serial_init(void) |
| 59 | { |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 60 | usart3_writel(CR, USART3_BIT(RSTRX) | USART3_BIT(RSTTX)); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 61 | |
| 62 | serial_setbrg(); |
| 63 | |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 64 | usart3_writel(CR, USART3_BIT(RXEN) | USART3_BIT(TXEN)); |
| 65 | usart3_writel(MR, (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))); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | void serial_putc(char c) |
| 75 | { |
| 76 | if (c == '\n') |
| 77 | serial_putc('\r'); |
| 78 | |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 79 | while (!(usart3_readl(CSR) & USART3_BIT(TXRDY))) ; |
| 80 | usart3_writel(THR, c); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void serial_puts(const char *s) |
| 84 | { |
| 85 | while (*s) |
| 86 | serial_putc(*s++); |
| 87 | } |
| 88 | |
| 89 | int serial_getc(void) |
| 90 | { |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 91 | while (!(usart3_readl(CSR) & USART3_BIT(RXRDY))) ; |
| 92 | return usart3_readl(RHR); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int serial_tstc(void) |
| 96 | { |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 97 | return (usart3_readl(CSR) & USART3_BIT(RXRDY)) != 0; |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | #endif /* CONFIG_ATMEL_USART */ |