wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * serial.c -- KS8695 serial driver |
| 3 | * |
| 4 | * (C) Copyright 2004, Greg Ungerer <greg.ungerer@opengear.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
| 22 | #include <asm/arch/platform.h> |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 23 | #include <serial.h> |
| 24 | #include <linux/compiler.h> |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 25 | |
| 26 | #ifndef CONFIG_SERIAL1 |
| 27 | #error "Bad: you didn't configure serial ..." |
| 28 | #endif |
| 29 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 32 | /* |
| 33 | * Define the UART hardware register access structure. |
| 34 | */ |
| 35 | struct ks8695uart { |
| 36 | unsigned int RX; /* 0x00 - Receive data (r) */ |
| 37 | unsigned int TX; /* 0x04 - Transmit data (w) */ |
| 38 | unsigned int FCR; /* 0x08 - Fifo Control (r/w) */ |
| 39 | unsigned int LCR; /* 0x0c - Line Control (r/w) */ |
| 40 | unsigned int MCR; /* 0x10 - Modem Control (r/w) */ |
| 41 | unsigned int LSR; /* 0x14 - Line Status (r/w) */ |
| 42 | unsigned int MSR; /* 0x18 - Modem Status (r/w) */ |
| 43 | unsigned int BD; /* 0x1c - Baud Rate (r/w) */ |
| 44 | unsigned int SR; /* 0x20 - Status (r/w) */ |
| 45 | }; |
| 46 | |
| 47 | #define KS8695_UART_ADDR ((void *) (KS8695_IO_BASE + KS8695_UART_RX_BUFFER)) |
| 48 | #define KS8695_UART_CLK 25000000 |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | * Under some circumstances we want to be "quiet" and not issue any |
| 53 | * serial output - though we want u-boot to otherwise work and behave |
| 54 | * the same. By default be noisy. |
| 55 | */ |
| 56 | int serial_console = 1; |
| 57 | |
| 58 | |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 59 | static void ks8695_serial_setbrg(void) |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 60 | { |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 61 | volatile struct ks8695uart *uartp = KS8695_UART_ADDR; |
| 62 | |
| 63 | /* Set to global baud rate and 8 data bits, no parity, 1 stop bit*/ |
| 64 | uartp->BD = KS8695_UART_CLK / gd->baudrate; |
| 65 | uartp->LCR = KS8695_UART_LINEC_WLEN8; |
| 66 | } |
| 67 | |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 68 | static int ks8695_serial_init(void) |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 69 | { |
| 70 | serial_console = 1; |
| 71 | serial_setbrg(); |
| 72 | return 0; |
| 73 | } |
| 74 | |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 75 | static void ks8695_serial_raw_putc(const char c) |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 76 | { |
| 77 | volatile struct ks8695uart *uartp = KS8695_UART_ADDR; |
| 78 | int i; |
| 79 | |
| 80 | for (i = 0; (i < 0x100000); i++) { |
| 81 | if (uartp->LSR & KS8695_UART_LINES_TXFE) |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | uartp->TX = c; |
| 86 | } |
| 87 | |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 88 | static void ks8695_serial_putc(const char c) |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 89 | { |
| 90 | if (serial_console) { |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 91 | ks8695_serial_raw_putc(c); |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 92 | if (c == '\n') |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 93 | ks8695_serial_raw_putc('\r'); |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 97 | static int ks8695_serial_tstc(void) |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 98 | { |
| 99 | volatile struct ks8695uart *uartp = KS8695_UART_ADDR; |
| 100 | if (serial_console) |
| 101 | return ((uartp->LSR & KS8695_UART_LINES_RXFE) ? 1 : 0); |
| 102 | return 0; |
| 103 | } |
| 104 | |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 105 | static int ks8695_serial_getc(void) |
wdenk | a21ad7b | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 106 | { |
| 107 | volatile struct ks8695uart *uartp = KS8695_UART_ADDR; |
| 108 | |
| 109 | while ((uartp->LSR & KS8695_UART_LINES_RXFE) == 0) |
| 110 | ; |
| 111 | return (uartp->RX); |
| 112 | } |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 113 | |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 114 | static struct serial_device ks8695_serial_drv = { |
| 115 | .name = "ks8695_serial", |
| 116 | .start = ks8695_serial_init, |
| 117 | .stop = NULL, |
| 118 | .setbrg = ks8695_serial_setbrg, |
| 119 | .putc = ks8695_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 120 | .puts = default_serial_puts, |
Marek Vasut | 5ee4656 | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 121 | .getc = ks8695_serial_getc, |
| 122 | .tstc = ks8695_serial_tstc, |
| 123 | }; |
| 124 | |
| 125 | void ks8695_serial_initialize(void) |
| 126 | { |
| 127 | serial_register(&ks8695_serial_drv); |
| 128 | } |
| 129 | |
| 130 | __weak struct serial_device *default_serial_console(void) |
| 131 | { |
| 132 | return &ks8695_serial_drv; |
| 133 | } |