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