wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <command.h> |
| 26 | |
| 27 | #include <asm/mcfuart.h> |
| 28 | |
| 29 | #ifdef CONFIG_M5272 |
| 30 | #include <asm/m5272.h> |
| 31 | #endif |
| 32 | |
| 33 | #ifdef CONFIG_M5282 |
| 34 | #include <asm/m5282.h> |
| 35 | #endif |
| 36 | |
| 37 | #define DoubleClock(a) ((double)(CFG_CLK) / 32.0 / (double)(a)) |
| 38 | |
| 39 | void rs_serial_setbaudrate(int port,int baudrate) |
| 40 | { |
| 41 | #ifdef CONFIG_M5272 |
| 42 | volatile unsigned char *uartp; |
| 43 | double clock, fraction; |
| 44 | |
| 45 | if (port == 0) |
| 46 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 47 | else |
| 48 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2); |
| 49 | |
| 50 | clock = DoubleClock(baudrate); /* Set baud above */ |
| 51 | |
| 52 | fraction = ((clock - (int)clock) * 16.0) + 0.5; |
| 53 | |
| 54 | uartp[MCFUART_UBG1] = (((int)clock >> 8) & 0xff); /* set msb baud */ |
| 55 | uartp[MCFUART_UBG2] = ((int)clock & 0xff); /* set lsb baud */ |
| 56 | uartp[MCFUART_UFPD] = ((int)fraction & 0xf); /* set baud fraction adjust */ |
| 57 | #endif |
| 58 | }; |
| 59 | |
| 60 | void rs_serial_init(int port,int baudrate) |
| 61 | { |
| 62 | volatile unsigned char *uartp; |
| 63 | |
| 64 | /* |
| 65 | * Reset UART, get it into known state... |
| 66 | */ |
| 67 | if (port == 0) |
| 68 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 69 | else |
| 70 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2); |
| 71 | |
| 72 | uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */ |
| 73 | uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */ |
| 74 | uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */ |
| 75 | uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR; /* reset Error pointer */ |
| 76 | |
| 77 | /* |
| 78 | * Set port for CONSOLE_BAUD_RATE, 8 data bits, 1 stop bit, no parity. |
| 79 | */ |
| 80 | uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8; |
| 81 | uartp[MCFUART_UMR] = MCFUART_MR2_STOP1; |
| 82 | |
| 83 | rs_serial_setbaudrate(port,baudrate); |
| 84 | |
| 85 | uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER; |
| 86 | uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE; |
| 87 | |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | /****************************************************************************/ |
| 92 | /* |
| 93 | * Output a single character, using UART polled mode. |
| 94 | * This is used for console output. |
| 95 | */ |
| 96 | |
| 97 | void rs_put_char(char ch) |
| 98 | { |
| 99 | volatile unsigned char *uartp; |
| 100 | int i; |
| 101 | |
| 102 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 103 | |
| 104 | for (i = 0; (i < 0x10000); i++) { |
| 105 | if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) |
| 106 | break; |
| 107 | } |
| 108 | uartp[MCFUART_UTB] = ch; |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | int rs_is_char(void) |
| 113 | { |
| 114 | volatile unsigned char *uartp; |
| 115 | |
| 116 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 117 | return((uartp[MCFUART_USR] & MCFUART_USR_RXREADY) ? 1 : 0); |
| 118 | } |
| 119 | |
| 120 | int rs_get_char(void) |
| 121 | { |
| 122 | volatile unsigned char *uartp; |
| 123 | |
| 124 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 125 | return(uartp[MCFUART_URB]); |
| 126 | } |
| 127 | |
| 128 | void serial_setbrg(void) { |
| 129 | DECLARE_GLOBAL_DATA_PTR; |
| 130 | rs_serial_setbaudrate(0,gd->bd->bi_baudrate); |
| 131 | } |
| 132 | |
| 133 | int serial_init(void) { |
| 134 | DECLARE_GLOBAL_DATA_PTR; |
| 135 | rs_serial_init(0,gd->baudrate); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void serial_putc(const char c) { |
| 141 | if (c == '\n') |
| 142 | serial_putc ('\r'); |
| 143 | rs_put_char(c); |
| 144 | } |
| 145 | |
| 146 | void serial_puts (const char *s) { |
| 147 | while (*s) { |
| 148 | serial_putc(*s++); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | int serial_getc(void) { |
| 153 | while(!rs_is_char()); |
| 154 | return rs_get_char(); |
| 155 | } |
| 156 | |
| 157 | int serial_tstc() { |
| 158 | return rs_is_char(); |
| 159 | } |