wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Serial Port stuff - taken from Linux |
| 3 | * |
| 4 | * (C) Copyright 2002 |
| 5 | * MAZeT GmbH <www.mazet.de> |
| 6 | * Stephan Linz <linz@mazet.de>, <linz@li-pro.net> |
| 7 | * |
| 8 | * (c) 2004 |
| 9 | * IMMS gGmbH <www.imms.de> |
| 10 | * Thomas Elste <info@elste.org> |
| 11 | * |
| 12 | * See file CREDITS for list of people who contributed to this |
| 13 | * project. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <common.h> |
wdenk | 3953988 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 32 | #include <asm/hardware.h> |
| 33 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 36 | #define PORTA (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_PORTA)) |
Wolfgang Denk | 3df5bea | 2005-10-09 01:41:48 +0200 | [diff] [blame] | 37 | #if !defined(CONFIG_NETARM_NS7520) |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 38 | #define PORTB (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_PORTB)) |
Wolfgang Denk | 3df5bea | 2005-10-09 01:41:48 +0200 | [diff] [blame] | 39 | #else |
| 40 | #define PORTC (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_PORTC)) |
| 41 | #endif |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 42 | |
| 43 | /* wait until transmitter is ready for another character */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 44 | #define TXWAITRDY(registers) \ |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 45 | { \ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | ulong tmo = get_timer(0) + 1 * CONFIG_SYS_HZ; \ |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 47 | while (((registers)->status_a & NETARM_SER_STATA_TX_RDY) == 0 ) { \ |
| 48 | if (get_timer(0) > tmo) \ |
| 49 | break; \ |
| 50 | } \ |
| 51 | } |
| 52 | |
| 53 | |
Wolfgang Denk | 3df5bea | 2005-10-09 01:41:48 +0200 | [diff] [blame] | 54 | #ifndef CONFIG_UART1_CONSOLE |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 55 | volatile netarm_serial_channel_t *serial_reg_ch1 = get_serial_channel(0); |
| 56 | volatile netarm_serial_channel_t *serial_reg_ch2 = get_serial_channel(1); |
Wolfgang Denk | 3df5bea | 2005-10-09 01:41:48 +0200 | [diff] [blame] | 57 | #else |
| 58 | volatile netarm_serial_channel_t *serial_reg_ch1 = get_serial_channel(1); |
| 59 | volatile netarm_serial_channel_t *serial_reg_ch2 = get_serial_channel(0); |
| 60 | #endif |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 61 | |
| 62 | extern void _netarm_led_FAIL1(void); |
| 63 | |
| 64 | /* |
| 65 | * Setup both serial i/f with given baudrate |
| 66 | */ |
| 67 | void serial_setbrg (void) |
| 68 | { |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 69 | /* set 0 ... make sure pins are configured for serial */ |
Wolfgang Denk | 3df5bea | 2005-10-09 01:41:48 +0200 | [diff] [blame] | 70 | #if !defined(CONFIG_NETARM_NS7520) |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 71 | PORTA = PORTB = |
| 72 | NETARM_GEN_PORT_MODE (0xef) | NETARM_GEN_PORT_DIR (0xe0); |
Wolfgang Denk | 3df5bea | 2005-10-09 01:41:48 +0200 | [diff] [blame] | 73 | #else |
| 74 | PORTA = NETARM_GEN_PORT_MODE (0xef) | NETARM_GEN_PORT_DIR (0xe0); |
| 75 | PORTC = NETARM_GEN_PORT_CSF (0xef) | NETARM_GEN_PORT_MODE (0xef) | NETARM_GEN_PORT_DIR (0xe0); |
| 76 | #endif |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 77 | |
| 78 | /* first turn em off */ |
| 79 | serial_reg_ch1->ctrl_a = serial_reg_ch2->ctrl_a = 0; |
| 80 | |
| 81 | /* clear match register, we don't need it */ |
| 82 | serial_reg_ch1->rx_match = serial_reg_ch2->rx_match = 0; |
| 83 | |
| 84 | /* setup bit rate generator and rx buffer gap timer (1 byte only) */ |
| 85 | if ((gd->baudrate >= MIN_BAUD_RATE) |
| 86 | && (gd->baudrate <= MAX_BAUD_RATE)) { |
| 87 | serial_reg_ch1->bitrate = serial_reg_ch2->bitrate = |
| 88 | NETARM_SER_BR_X16 (gd->baudrate); |
| 89 | serial_reg_ch1->rx_buf_timer = serial_reg_ch2->rx_buf_timer = |
| 90 | 0; |
| 91 | serial_reg_ch1->rx_char_timer = serial_reg_ch2->rx_char_timer = |
| 92 | NETARM_SER_RXGAP (gd->baudrate); |
| 93 | } else { |
| 94 | hang (); |
| 95 | } |
| 96 | |
| 97 | /* setup port mode */ |
| 98 | serial_reg_ch1->ctrl_b = serial_reg_ch2->ctrl_b = |
| 99 | ( NETARM_SER_CTLB_RCGT_EN | |
| 100 | NETARM_SER_CTLB_UART_MODE); |
| 101 | serial_reg_ch1->ctrl_a = serial_reg_ch2->ctrl_a = |
| 102 | ( NETARM_SER_CTLA_ENABLE | |
| 103 | NETARM_SER_CTLA_P_NONE | |
| 104 | /* see errata */ |
| 105 | NETARM_SER_CTLA_2STOP | |
| 106 | NETARM_SER_CTLA_8BITS | |
| 107 | NETARM_SER_CTLA_DTR_EN | |
| 108 | NETARM_SER_CTLA_RTS_EN); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /* |
| 113 | * Initialise the serial port with the given baudrate. The settings |
| 114 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 115 | */ |
| 116 | int serial_init (void) |
| 117 | { |
| 118 | serial_setbrg (); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | * Output a single byte to the serial port. |
| 125 | */ |
| 126 | void serial_putc (const char c) |
| 127 | { |
| 128 | volatile unsigned char *fifo; |
| 129 | |
| 130 | /* If \n, also do \r */ |
| 131 | if (c == '\n') |
| 132 | serial_putc ('\r'); |
| 133 | |
| 134 | fifo = (volatile unsigned char *) &(serial_reg_ch1->fifo); |
| 135 | TXWAITRDY (serial_reg_ch1); |
| 136 | *fifo = c; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Test of a single byte from the serial port. Returns 1 on success, 0 |
| 141 | * otherwise. |
| 142 | */ |
| 143 | int serial_tstc(void) |
| 144 | { |
| 145 | return serial_reg_ch1->status_a & NETARM_SER_STATA_RX_RDY; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 150 | * otherwise. |
| 151 | */ |
| 152 | int serial_getc (void) |
| 153 | { |
| 154 | unsigned int ch_uint; |
| 155 | volatile unsigned int *fifo; |
| 156 | volatile unsigned char *fifo_char = NULL; |
| 157 | int buf_count = 0; |
| 158 | |
| 159 | while (!(serial_reg_ch1->status_a & NETARM_SER_STATA_RX_RDY)) |
| 160 | /* NOP */ ; |
| 161 | |
| 162 | fifo = (volatile unsigned int *) &(serial_reg_ch1->fifo); |
| 163 | fifo_char = (unsigned char *) &ch_uint; |
| 164 | ch_uint = *fifo; |
| 165 | |
| 166 | buf_count = NETARM_SER_STATA_RXFDB (serial_reg_ch1->status_a); |
| 167 | switch (buf_count) { |
| 168 | case NETARM_SER_STATA_RXFDB_4BYTES: |
| 169 | buf_count = 4; |
| 170 | break; |
| 171 | case NETARM_SER_STATA_RXFDB_3BYTES: |
| 172 | buf_count = 3; |
| 173 | break; |
| 174 | case NETARM_SER_STATA_RXFDB_2BYTES: |
| 175 | buf_count = 2; |
| 176 | break; |
| 177 | case NETARM_SER_STATA_RXFDB_1BYTES: |
| 178 | buf_count = 1; |
| 179 | break; |
| 180 | default: |
| 181 | /* panic, be never here */ |
wdenk | e86e5a0 | 2004-10-17 21:12:06 +0000 | [diff] [blame] | 182 | break; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | serial_reg_ch1->status_a |= NETARM_SER_STATA_RX_CLOSED; |
| 186 | |
| 187 | return ch_uint & 0xff; |
| 188 | } |
| 189 | |
| 190 | void serial_puts (const char *s) |
| 191 | { |
| 192 | while (*s) { |
| 193 | serial_putc (*s++); |
| 194 | } |
| 195 | } |