wdenk | e65527f | 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> |
Wolfgang Denk | da06a33 | 2006-05-09 13:49:12 +0200 | [diff] [blame^] | 26 | #include <watchdog.h> |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 27 | |
| 28 | #include <asm/mcfuart.h> |
| 29 | |
Zachary P. Landau | 0bba862 | 2006-01-26 17:35:56 -0500 | [diff] [blame] | 30 | #ifdef CONFIG_M5271 |
| 31 | #include <asm/m5271.h> |
| 32 | #endif |
| 33 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 34 | #ifdef CONFIG_M5272 |
| 35 | #include <asm/m5272.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifdef CONFIG_M5282 |
| 39 | #include <asm/m5282.h> |
| 40 | #endif |
| 41 | |
stroese | 53395a2 | 2004-12-16 18:09:49 +0000 | [diff] [blame] | 42 | #ifdef CONFIG_M5249 |
| 43 | #include <asm/m5249.h> |
| 44 | #endif |
| 45 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 46 | DECLARE_GLOBAL_DATA_PTR; |
| 47 | |
Marian Balakowicz | a3076c8 | 2006-05-09 11:37:13 +0200 | [diff] [blame] | 48 | #if defined(CONFIG_M5249) || defined(CONFIG_M5271) |
stroese | 53395a2 | 2004-12-16 18:09:49 +0000 | [diff] [blame] | 49 | #define DoubleClock(a) ((double)(CFG_CLK/2) / 32.0 / (double)(a)) |
| 50 | #else |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 51 | #define DoubleClock(a) ((double)(CFG_CLK) / 32.0 / (double)(a)) |
stroese | 53395a2 | 2004-12-16 18:09:49 +0000 | [diff] [blame] | 52 | #endif |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 53 | |
| 54 | void rs_serial_setbaudrate(int port,int baudrate) |
| 55 | { |
Zachary P. Landau | 0bba862 | 2006-01-26 17:35:56 -0500 | [diff] [blame] | 56 | #if defined(CONFIG_M5272) || defined(CONFIG_M5249) || defined(CONFIG_M5271) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 57 | volatile unsigned char *uartp; |
Marian Balakowicz | a3076c8 | 2006-05-09 11:37:13 +0200 | [diff] [blame] | 58 | #ifndef CONFIG_M5271 |
| 59 | double fraction; |
| 60 | #endif |
| 61 | double clock; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 62 | |
| 63 | if (port == 0) |
| 64 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 65 | else |
| 66 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2); |
| 67 | |
| 68 | clock = DoubleClock(baudrate); /* Set baud above */ |
| 69 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 70 | uartp[MCFUART_UBG1] = (((int)clock >> 8) & 0xff); /* set msb baud */ |
| 71 | uartp[MCFUART_UBG2] = ((int)clock & 0xff); /* set lsb baud */ |
Marian Balakowicz | a3076c8 | 2006-05-09 11:37:13 +0200 | [diff] [blame] | 72 | |
Zachary P. Landau | 0bba862 | 2006-01-26 17:35:56 -0500 | [diff] [blame] | 73 | #ifndef CONFIG_M5271 |
Marian Balakowicz | a3076c8 | 2006-05-09 11:37:13 +0200 | [diff] [blame] | 74 | fraction = ((clock - (int)clock) * 16.0) + 0.5; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 75 | uartp[MCFUART_UFPD] = ((int)fraction & 0xf); /* set baud fraction adjust */ |
| 76 | #endif |
Zachary P. Landau | 0bba862 | 2006-01-26 17:35:56 -0500 | [diff] [blame] | 77 | #endif |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | void rs_serial_init(int port,int baudrate) |
| 81 | { |
| 82 | volatile unsigned char *uartp; |
| 83 | |
| 84 | /* |
| 85 | * Reset UART, get it into known state... |
| 86 | */ |
| 87 | if (port == 0) |
| 88 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 89 | else |
| 90 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2); |
| 91 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 92 | uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */ |
Marian Balakowicz | a3076c8 | 2006-05-09 11:37:13 +0200 | [diff] [blame] | 93 | uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */ |
| 94 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 95 | uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */ |
| 96 | uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR; /* reset Error pointer */ |
| 97 | |
| 98 | /* |
| 99 | * Set port for CONSOLE_BAUD_RATE, 8 data bits, 1 stop bit, no parity. |
| 100 | */ |
| 101 | uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8; |
| 102 | uartp[MCFUART_UMR] = MCFUART_MR2_STOP1; |
| 103 | |
Marian Balakowicz | a3076c8 | 2006-05-09 11:37:13 +0200 | [diff] [blame] | 104 | /* Mask UART interrupts */ |
| 105 | uartp[MCFUART_UIMR] = 0; |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 106 | |
Marian Balakowicz | a3076c8 | 2006-05-09 11:37:13 +0200 | [diff] [blame] | 107 | /* Set clock Select Register: Tx/Rx clock is timer */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 108 | uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER; |
Marian Balakowicz | a3076c8 | 2006-05-09 11:37:13 +0200 | [diff] [blame] | 109 | |
| 110 | rs_serial_setbaudrate(port,baudrate); |
| 111 | |
| 112 | /* Enable Tx/Rx */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 113 | uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE; |
| 114 | |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | /****************************************************************************/ |
| 119 | /* |
| 120 | * Output a single character, using UART polled mode. |
| 121 | * This is used for console output. |
| 122 | */ |
| 123 | |
| 124 | void rs_put_char(char ch) |
| 125 | { |
| 126 | volatile unsigned char *uartp; |
| 127 | int i; |
| 128 | |
| 129 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 130 | |
| 131 | for (i = 0; (i < 0x10000); i++) { |
| 132 | if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) |
| 133 | break; |
| 134 | } |
| 135 | uartp[MCFUART_UTB] = ch; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | int rs_is_char(void) |
| 140 | { |
| 141 | volatile unsigned char *uartp; |
| 142 | |
| 143 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 144 | return((uartp[MCFUART_USR] & MCFUART_USR_RXREADY) ? 1 : 0); |
| 145 | } |
| 146 | |
| 147 | int rs_get_char(void) |
| 148 | { |
| 149 | volatile unsigned char *uartp; |
| 150 | |
| 151 | uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1); |
| 152 | return(uartp[MCFUART_URB]); |
| 153 | } |
| 154 | |
| 155 | void serial_setbrg(void) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 156 | rs_serial_setbaudrate(0,gd->bd->bi_baudrate); |
| 157 | } |
| 158 | |
| 159 | int serial_init(void) { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 160 | rs_serial_init(0,gd->baudrate); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void serial_putc(const char c) { |
| 166 | if (c == '\n') |
| 167 | serial_putc ('\r'); |
| 168 | rs_put_char(c); |
| 169 | } |
| 170 | |
| 171 | void serial_puts (const char *s) { |
| 172 | while (*s) { |
| 173 | serial_putc(*s++); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | int serial_getc(void) { |
Wolfgang Denk | da06a33 | 2006-05-09 13:49:12 +0200 | [diff] [blame^] | 178 | while(!rs_is_char()) |
| 179 | WATCHDOG_RESET(); |
| 180 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 181 | return rs_get_char(); |
| 182 | } |
| 183 | |
| 184 | int serial_tstc() { |
| 185 | return rs_is_char(); |
| 186 | } |