wdenk | c8434db | 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> |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame^] | 33 | |
| 34 | #ifndef CONFIG_AT91_LEGACY |
wdenk | 8dba050 | 2003-03-31 16:34:49 +0000 | [diff] [blame] | 35 | #include <asm/io.h> |
wdenk | bb2d927 | 2003-06-25 22:26:29 +0000 | [diff] [blame] | 36 | #include <asm/arch/hardware.h> |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame^] | 37 | #define CONFIG_AT91_LEGACY |
| 38 | #include <asm/arch-at91rm9200/AT91RM9200.h> |
| 39 | #warning Please update to use C structur SoC access ! |
| 40 | #else |
| 41 | #include <asm/arch/AT91RM9200.h> |
| 42 | #endif |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 43 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 44 | DECLARE_GLOBAL_DATA_PTR; |
| 45 | |
wdenk | 70ae5b4 | 2004-10-10 17:05:18 +0000 | [diff] [blame] | 46 | #if !defined(CONFIG_DBGU) && !defined(CONFIG_USART0) && !defined(CONFIG_USART1) |
| 47 | #error must define one of CONFIG_DBGU or CONFIG_USART0 or CONFIG_USART1 |
wdenk | b2fc7e1 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 50 | /* ggi thunder */ |
wdenk | b2fc7e1 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 51 | #ifdef CONFIG_DBGU |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 52 | AT91PS_USART us = (AT91PS_USART) AT91C_BASE_DBGU; |
wdenk | b2fc7e1 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 53 | #endif |
wdenk | 70ae5b4 | 2004-10-10 17:05:18 +0000 | [diff] [blame] | 54 | #ifdef CONFIG_USART0 |
| 55 | AT91PS_USART us = (AT91PS_USART) AT91C_BASE_US0; |
| 56 | #endif |
wdenk | b2fc7e1 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 57 | #ifdef CONFIG_USART1 |
| 58 | AT91PS_USART us = (AT91PS_USART) AT91C_BASE_US1; |
| 59 | #endif |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 60 | |
wdenk | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 61 | void serial_setbrg (void) |
| 62 | { |
wdenk | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 63 | int baudrate; |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 64 | |
wdenk | 70ae5b4 | 2004-10-10 17:05:18 +0000 | [diff] [blame] | 65 | if ((baudrate = gd->baudrate) <= 0) |
wdenk | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 66 | baudrate = CONFIG_BAUDRATE; |
wdenk | f456993 | 2005-04-20 12:36:05 +0000 | [diff] [blame] | 67 | /* MASTER_CLOCK/(16 * baudrate) */ |
Wolfgang Denk | e25f052 | 2005-08-12 22:35:59 +0200 | [diff] [blame] | 68 | us->US_BRGR = (AT91C_MASTER_CLOCK >> 4) / (unsigned)baudrate; |
wdenk | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 69 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 70 | |
wdenk | 0ef4946 | 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 | b2fc7e1 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 74 | #ifdef CONFIG_DBGU |
wdenk | 0ef4946 | 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 | b2fc7e1 | 2004-09-21 23:33:32 +0000 | [diff] [blame] | 77 | #endif |
wdenk | 70ae5b4 | 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 | b2fc7e1 | 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 | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 86 | serial_setbrg (); |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 87 | |
wdenk | 0ef4946 | 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 | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 96 | |
Jean-Christophe PLAGNIOL-VILLARD | 23a9c97 | 2009-03-27 23:26:43 +0100 | [diff] [blame] | 97 | void serial_exit (void) |
| 98 | { |
| 99 | us->US_CR = (AT91C_US_RSTRX | AT91C_US_RSTTX); |
| 100 | } |
| 101 | |
wdenk | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 102 | void serial_putc (const char c) |
| 103 | { |
| 104 | if (c == '\n') |
| 105 | serial_putc ('\r'); |
| 106 | while ((us->US_CSR & AT91C_US_TXRDY) == 0); |
| 107 | us->US_THR = c; |
| 108 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 109 | |
wdenk | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 110 | void serial_puts (const char *s) |
| 111 | { |
| 112 | while (*s) { |
| 113 | serial_putc (*s++); |
| 114 | } |
| 115 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 116 | |
wdenk | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 117 | int serial_getc (void) |
| 118 | { |
| 119 | while ((us->US_CSR & AT91C_US_RXRDY) == 0); |
| 120 | return us->US_RHR; |
| 121 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 122 | |
wdenk | 0ef4946 | 2004-03-15 09:00:01 +0000 | [diff] [blame] | 123 | int serial_tstc (void) |
| 124 | { |
| 125 | return ((us->US_CSR & AT91C_US_RXRDY) == AT91C_US_RXRDY); |
| 126 | } |