wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Lineo, Inc <www.lineo.com> |
| 4 | * Bernhard Kuhn <bkuhn@lineo.com> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Marius Groeger <mgroeger@sysgo.de> |
| 9 | * |
| 10 | * (C) Copyright 2002 |
| 11 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 12 | * Alex Zuepke <azu@sysgo.de> |
| 13 | * |
| 14 | * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <common.h> |
wdenk | 85ec0bc | 2003-03-31 16:34:49 +0000 | [diff] [blame] | 33 | #include <asm/io.h> |
wdenk | b783eda | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 34 | #include <asm/arch/hardware.h> |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 35 | |
wdenk | 2cbe571 | 2004-10-10 17:05:18 +0000 | [diff] [blame] | 36 | #if !defined(CONFIG_DBGU) && !defined(CONFIG_USART0) && !defined(CONFIG_USART1) |
| 37 | #error must define one of CONFIG_DBGU or CONFIG_USART0 or CONFIG_USART1 |
wdenk | 4734cb7 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 40 | /* ggi thunder */ |
wdenk | 4734cb7 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 41 | #ifdef CONFIG_DBGU |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 42 | AT91PS_USART us = (AT91PS_USART) AT91C_BASE_DBGU; |
wdenk | 4734cb7 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 43 | #endif |
wdenk | 2cbe571 | 2004-10-10 17:05:18 +0000 | [diff] [blame] | 44 | #ifdef CONFIG_USART0 |
| 45 | AT91PS_USART us = (AT91PS_USART) AT91C_BASE_US0; |
| 46 | #endif |
wdenk | 4734cb7 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 47 | #ifdef CONFIG_USART1 |
| 48 | AT91PS_USART us = (AT91PS_USART) AT91C_BASE_US1; |
| 49 | #endif |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 50 | |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 51 | void serial_setbrg (void) |
| 52 | { |
| 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | int baudrate; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 55 | |
wdenk | 2cbe571 | 2004-10-10 17:05:18 +0000 | [diff] [blame] | 56 | if ((baudrate = gd->baudrate) <= 0) |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 57 | baudrate = CONFIG_BAUDRATE; |
wdenk | 2cbe571 | 2004-10-10 17:05:18 +0000 | [diff] [blame] | 58 | if (baudrate == CONFIG_BAUDRATE) { |
| 59 | us->US_BRGR = CFG_AT91C_BRGR_DIVISOR; /* hardcode so no __divsi3 */ |
| 60 | } else { |
| 61 | #if 0 |
| 62 | /* 33 -> 115200 */ |
| 63 | us->US_BRGR = 33 * (115200/baudrate); |
| 64 | #else |
| 65 | /* MASTER_CLOCK/(16 * baudrate) */ |
| 66 | us->US_BRGR = (AT91C_MASTER_CLOCK >> 4)/baudrate; |
| 67 | #endif |
| 68 | } |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 69 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 70 | |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 71 | int serial_init (void) |
| 72 | { |
| 73 | /* make any port initializations specific to this port */ |
wdenk | 4734cb7 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 74 | #ifdef CONFIG_DBGU |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 75 | *AT91C_PIOA_PDR = AT91C_PA31_DTXD | AT91C_PA30_DRXD; /* PA 31 & 30 */ |
| 76 | *AT91C_PMC_PCER = 1 << AT91C_ID_SYS; /* enable clock */ |
wdenk | 4734cb7 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 77 | #endif |
wdenk | 2cbe571 | 2004-10-10 17:05:18 +0000 | [diff] [blame] | 78 | #ifdef CONFIG_USART0 |
| 79 | *AT91C_PIOA_PDR = AT91C_PA17_TXD0 | AT91C_PA18_RXD0; |
| 80 | *AT91C_PMC_PCER |= 1 << AT91C_ID_USART0; /* enable clock */ |
| 81 | #endif |
wdenk | 4734cb7 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 82 | #ifdef CONFIG_USART1 |
| 83 | *AT91C_PIOB_PDR = AT91C_PB21_TXD1 | AT91C_PB20_RXD1; |
| 84 | *AT91C_PMC_PCER |= 1 << AT91C_ID_USART1; /* enable clock */ |
| 85 | #endif |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 86 | serial_setbrg (); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 87 | |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 88 | us->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX; |
| 89 | us->US_CR = AT91C_US_RXEN | AT91C_US_TXEN; |
| 90 | us->US_MR = |
| 91 | (AT91C_US_CLKS_CLOCK | AT91C_US_CHRL_8_BITS | |
| 92 | AT91C_US_PAR_NONE | AT91C_US_NBSTOP_1_BIT); |
| 93 | us->US_IMR = ~0ul; |
| 94 | return (0); |
| 95 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 96 | |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 97 | void serial_putc (const char c) |
| 98 | { |
| 99 | if (c == '\n') |
| 100 | serial_putc ('\r'); |
| 101 | while ((us->US_CSR & AT91C_US_TXRDY) == 0); |
| 102 | us->US_THR = c; |
| 103 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 104 | |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 105 | void serial_puts (const char *s) |
| 106 | { |
| 107 | while (*s) { |
| 108 | serial_putc (*s++); |
| 109 | } |
| 110 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 111 | |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 112 | int serial_getc (void) |
| 113 | { |
| 114 | while ((us->US_CSR & AT91C_US_RXRDY) == 0); |
| 115 | return us->US_RHR; |
| 116 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 117 | |
wdenk | d9df1f4 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 118 | int serial_tstc (void) |
| 119 | { |
| 120 | return ((us->US_CSR & AT91C_US_RXRDY) == AT91C_US_RXRDY); |
| 121 | } |