wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
Detlev Zundel | f1b3f2b | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 3 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
| 22 | #include <lh7a40x.h> |
| 23 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 26 | #if defined(CONFIG_CONSOLE_UART1) |
| 27 | # define UART_CONSOLE 1 |
| 28 | #elif defined(CONFIG_CONSOLE_UART2) |
| 29 | # define UART_CONSOLE 2 |
| 30 | #elif defined(CONFIG_CONSOLE_UART3) |
| 31 | # define UART_CONSOLE 3 |
| 32 | #else |
| 33 | # error "No console configured ... " |
| 34 | #endif |
| 35 | |
Marek Vasut | 58177b7 | 2012-09-14 22:36:27 +0200 | [diff] [blame] | 36 | static void lh7a40x_serial_setbrg(void) |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 37 | { |
wdenk | 37bdfdf | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 38 | lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE); |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 39 | int i; |
| 40 | unsigned int reg = 0; |
| 41 | |
| 42 | /* |
| 43 | * userguide 15.1.2.4 |
| 44 | * |
| 45 | * BAUDDIV is (UART_REF_FREQ/(16 X BAUD))-1 |
| 46 | * |
| 47 | * UART_REF_FREQ = external system clock input / 2 (Hz) |
| 48 | * BAUD is desired baudrate (bits/s) |
| 49 | * |
| 50 | * NOTE: we add (divisor/2) to numerator to round for |
| 51 | * more precision |
| 52 | */ |
| 53 | reg = (((get_PLLCLK()/2) + ((16*gd->baudrate)/2)) / (16 * gd->baudrate)) - 1; |
| 54 | uart->brcon = reg; |
| 55 | |
| 56 | for (i = 0; i < 100; i++); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Initialise the serial port with the given baudrate. The settings |
| 61 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 62 | * |
| 63 | */ |
Marek Vasut | 58177b7 | 2012-09-14 22:36:27 +0200 | [diff] [blame] | 64 | static int lh7a40x_serial_init(void) |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 65 | { |
wdenk | 37bdfdf | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 66 | lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE); |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 67 | |
| 68 | /* UART must be enabled before writing to any config registers */ |
| 69 | uart->con |= (UART_EN); |
| 70 | |
| 71 | #ifdef CONFIG_CONSOLE_UART1 |
| 72 | /* infrared disabled */ |
| 73 | uart->con |= UART_SIRD; |
| 74 | #endif |
| 75 | /* loopback disabled */ |
| 76 | uart->con &= ~(UART_LBE); |
| 77 | |
| 78 | /* modem lines and tx/rx polarities */ |
| 79 | uart->con &= ~(UART_MXP | UART_TXP | UART_RXP); |
| 80 | |
| 81 | /* FIFO enable, N81 */ |
| 82 | uart->fcon = (UART_WLEN_8 | UART_FEN | UART_STP2_1); |
| 83 | |
| 84 | /* set baudrate */ |
| 85 | serial_setbrg (); |
| 86 | |
| 87 | /* enable rx interrupt */ |
| 88 | uart->inten |= UART_RI; |
| 89 | |
| 90 | return (0); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 95 | * otherwise. When the function is succesfull, the character read is |
| 96 | * written into its argument c. |
| 97 | */ |
Marek Vasut | 58177b7 | 2012-09-14 22:36:27 +0200 | [diff] [blame] | 98 | static int lh7a40x_serial_getc(void) |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 99 | { |
wdenk | 37bdfdf | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 100 | lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE); |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 101 | |
| 102 | /* wait for character to arrive */ |
| 103 | while (uart->status & UART_RXFE); |
| 104 | |
| 105 | return(uart->data & 0xff); |
| 106 | } |
| 107 | |
| 108 | #ifdef CONFIG_HWFLOW |
| 109 | static int hwflow = 0; /* turned off by default */ |
| 110 | int hwflow_onoff(int on) |
| 111 | { |
| 112 | switch(on) { |
| 113 | case 0: |
| 114 | default: |
| 115 | break; /* return current */ |
| 116 | case 1: |
| 117 | hwflow = 1; /* turn on */ |
| 118 | break; |
| 119 | case -1: |
| 120 | hwflow = 0; /* turn off */ |
| 121 | break; |
| 122 | } |
| 123 | return hwflow; |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | #ifdef CONFIG_MODEM_SUPPORT |
| 128 | static int be_quiet = 0; |
| 129 | void disable_putc(void) |
| 130 | { |
| 131 | be_quiet = 1; |
| 132 | } |
| 133 | |
| 134 | void enable_putc(void) |
| 135 | { |
| 136 | be_quiet = 0; |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Output a single byte to the serial port. |
| 143 | */ |
Marek Vasut | 58177b7 | 2012-09-14 22:36:27 +0200 | [diff] [blame] | 144 | static void lh7a40x_serial_putc(const char c) |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 145 | { |
wdenk | 37bdfdf | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 146 | lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE); |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 147 | |
| 148 | #ifdef CONFIG_MODEM_SUPPORT |
| 149 | if (be_quiet) |
| 150 | return; |
| 151 | #endif |
| 152 | |
| 153 | /* wait for room in the tx FIFO */ |
| 154 | while (!(uart->status & UART_TXFE)); |
| 155 | |
| 156 | #ifdef CONFIG_HWFLOW |
| 157 | /* Wait for CTS up */ |
| 158 | while(hwflow && !(uart->status & UART_CTS)); |
| 159 | #endif |
| 160 | |
| 161 | uart->data = c; |
| 162 | |
| 163 | /* If \n, also do \r */ |
| 164 | if (c == '\n') |
| 165 | serial_putc ('\r'); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Test whether a character is in the RX buffer |
| 170 | */ |
Marek Vasut | 58177b7 | 2012-09-14 22:36:27 +0200 | [diff] [blame] | 171 | static int lh7a40x_serial_tstc(void) |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 172 | { |
wdenk | 37bdfdf | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 173 | lh7a40x_uart_t* uart = LH7A40X_UART_PTR(UART_CONSOLE); |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 174 | |
| 175 | return(!(uart->status & UART_RXFE)); |
| 176 | } |
| 177 | |
Marek Vasut | 58177b7 | 2012-09-14 22:36:27 +0200 | [diff] [blame] | 178 | static void lh7a40x_serial_puts(const char *s) |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 179 | { |
| 180 | while (*s) { |
| 181 | serial_putc (*s++); |
| 182 | } |
| 183 | } |
Marek Vasut | 58177b7 | 2012-09-14 22:36:27 +0200 | [diff] [blame] | 184 | |
Marek Vasut | 58177b7 | 2012-09-14 22:36:27 +0200 | [diff] [blame] | 185 | static struct serial_device lh7a40x_serial_drv = { |
| 186 | .name = "lh7a40x_serial", |
| 187 | .start = lh7a40x_serial_init, |
| 188 | .stop = NULL, |
| 189 | .setbrg = lh7a40x_serial_setbrg, |
| 190 | .putc = lh7a40x_serial_putc, |
| 191 | .puts = lh7a40x_serial_puts, |
| 192 | .getc = lh7a40x_serial_getc, |
| 193 | .tstc = lh7a40x_serial_tstc, |
| 194 | }; |
| 195 | |
| 196 | void lh7a40x_serial_initialize(void) |
| 197 | { |
| 198 | serial_register(&lh7a40x_serial_drv); |
| 199 | } |
| 200 | |
| 201 | __weak struct serial_device *default_serial_console(void) |
| 202 | { |
| 203 | return &lh7a40x_serial_drv; |
| 204 | } |