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> |
Jens Scharsig | 0cf0b93 | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 19 | #ifndef CONFIG_AT91_LEGACY |
| 20 | #define CONFIG_AT91_LEGACY |
| 21 | #warning Please update to use C structur SoC access ! |
| 22 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 843a265 | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 23 | #include <watchdog.h> |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 24 | |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 25 | #include <asm/io.h> |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 26 | #include <asm/arch/clk.h> |
| 27 | #include <asm/arch/memory-map.h> |
| 28 | |
| 29 | #if defined(CONFIG_USART0) |
| 30 | # define USART_ID 0 |
| 31 | # define USART_BASE USART0_BASE |
| 32 | #elif defined(CONFIG_USART1) |
| 33 | # define USART_ID 1 |
| 34 | # define USART_BASE USART1_BASE |
| 35 | #elif defined(CONFIG_USART2) |
| 36 | # define USART_ID 2 |
| 37 | # define USART_BASE USART2_BASE |
| 38 | #elif defined(CONFIG_USART3) |
| 39 | # define USART_ID 3 |
| 40 | # define USART_BASE USART3_BASE |
| 41 | #endif |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 42 | |
| 43 | #include "atmel_usart.h" |
| 44 | |
| 45 | DECLARE_GLOBAL_DATA_PTR; |
| 46 | |
| 47 | void serial_setbrg(void) |
| 48 | { |
| 49 | unsigned long divisor; |
| 50 | unsigned long usart_hz; |
| 51 | |
| 52 | /* |
| 53 | * Master Clock |
| 54 | * Baud Rate = -------------- |
| 55 | * 16 * CD |
| 56 | */ |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 57 | usart_hz = get_usart_clk_rate(USART_ID); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 58 | divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate; |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 59 | usart3_writel(BRGR, USART3_BF(CD, divisor)); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int serial_init(void) |
| 63 | { |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 64 | usart3_writel(CR, USART3_BIT(RSTRX) | USART3_BIT(RSTTX)); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 65 | |
| 66 | serial_setbrg(); |
| 67 | |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 68 | usart3_writel(CR, USART3_BIT(RXEN) | USART3_BIT(TXEN)); |
| 69 | usart3_writel(MR, (USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
| 70 | | USART3_BF(USCLKS, USART3_USCLKS_MCK) |
| 71 | | USART3_BF(CHRL, USART3_CHRL_8) |
| 72 | | USART3_BF(PAR, USART3_PAR_NONE) |
| 73 | | USART3_BF(NBSTOP, USART3_NBSTOP_1))); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | void serial_putc(char c) |
| 79 | { |
| 80 | if (c == '\n') |
| 81 | serial_putc('\r'); |
| 82 | |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 83 | while (!(usart3_readl(CSR) & USART3_BIT(TXRDY))) ; |
| 84 | usart3_writel(THR, c); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 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 | { |
Jean-Christophe PLAGNIOL-VILLARD | 843a265 | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 95 | while (!(usart3_readl(CSR) & USART3_BIT(RXRDY))) |
| 96 | WATCHDOG_RESET(); |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 97 | return usart3_readl(RHR); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | int serial_tstc(void) |
| 101 | { |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 102 | return (usart3_readl(CSR) & USART3_BIT(RXRDY)) != 0; |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 103 | } |