wdenk | 3a574cb | 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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <asm/arch/platform.h> |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 11 | #include <serial.h> |
| 12 | #include <linux/compiler.h> |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 13 | |
| 14 | #ifndef CONFIG_SERIAL1 |
| 15 | #error "Bad: you didn't configure serial ..." |
| 16 | #endif |
| 17 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Define the UART hardware register access structure. |
| 22 | */ |
| 23 | struct ks8695uart { |
| 24 | unsigned int RX; /* 0x00 - Receive data (r) */ |
| 25 | unsigned int TX; /* 0x04 - Transmit data (w) */ |
| 26 | unsigned int FCR; /* 0x08 - Fifo Control (r/w) */ |
| 27 | unsigned int LCR; /* 0x0c - Line Control (r/w) */ |
| 28 | unsigned int MCR; /* 0x10 - Modem Control (r/w) */ |
| 29 | unsigned int LSR; /* 0x14 - Line Status (r/w) */ |
| 30 | unsigned int MSR; /* 0x18 - Modem Status (r/w) */ |
| 31 | unsigned int BD; /* 0x1c - Baud Rate (r/w) */ |
| 32 | unsigned int SR; /* 0x20 - Status (r/w) */ |
| 33 | }; |
| 34 | |
| 35 | #define KS8695_UART_ADDR ((void *) (KS8695_IO_BASE + KS8695_UART_RX_BUFFER)) |
| 36 | #define KS8695_UART_CLK 25000000 |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * Under some circumstances we want to be "quiet" and not issue any |
| 41 | * serial output - though we want u-boot to otherwise work and behave |
| 42 | * the same. By default be noisy. |
| 43 | */ |
| 44 | int serial_console = 1; |
| 45 | |
| 46 | |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 47 | static void ks8695_serial_setbrg(void) |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 48 | { |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 49 | volatile struct ks8695uart *uartp = KS8695_UART_ADDR; |
| 50 | |
| 51 | /* Set to global baud rate and 8 data bits, no parity, 1 stop bit*/ |
| 52 | uartp->BD = KS8695_UART_CLK / gd->baudrate; |
| 53 | uartp->LCR = KS8695_UART_LINEC_WLEN8; |
| 54 | } |
| 55 | |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 56 | static int ks8695_serial_init(void) |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 57 | { |
| 58 | serial_console = 1; |
| 59 | serial_setbrg(); |
| 60 | return 0; |
| 61 | } |
| 62 | |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 63 | static void ks8695_serial_raw_putc(const char c) |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 64 | { |
| 65 | volatile struct ks8695uart *uartp = KS8695_UART_ADDR; |
| 66 | int i; |
| 67 | |
| 68 | for (i = 0; (i < 0x100000); i++) { |
| 69 | if (uartp->LSR & KS8695_UART_LINES_TXFE) |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | uartp->TX = c; |
| 74 | } |
| 75 | |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 76 | static void ks8695_serial_putc(const char c) |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 77 | { |
| 78 | if (serial_console) { |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 79 | ks8695_serial_raw_putc(c); |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 80 | if (c == '\n') |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 81 | ks8695_serial_raw_putc('\r'); |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 85 | static int ks8695_serial_tstc(void) |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 86 | { |
| 87 | volatile struct ks8695uart *uartp = KS8695_UART_ADDR; |
| 88 | if (serial_console) |
| 89 | return ((uartp->LSR & KS8695_UART_LINES_RXFE) ? 1 : 0); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 93 | static int ks8695_serial_getc(void) |
wdenk | 3a574cb | 2005-05-19 22:39:42 +0000 | [diff] [blame] | 94 | { |
| 95 | volatile struct ks8695uart *uartp = KS8695_UART_ADDR; |
| 96 | |
| 97 | while ((uartp->LSR & KS8695_UART_LINES_RXFE) == 0) |
| 98 | ; |
| 99 | return (uartp->RX); |
| 100 | } |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 101 | |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 102 | static struct serial_device ks8695_serial_drv = { |
| 103 | .name = "ks8695_serial", |
| 104 | .start = ks8695_serial_init, |
| 105 | .stop = NULL, |
| 106 | .setbrg = ks8695_serial_setbrg, |
| 107 | .putc = ks8695_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 108 | .puts = default_serial_puts, |
Marek Vasut | a7ffb2f | 2012-09-14 22:35:43 +0200 | [diff] [blame] | 109 | .getc = ks8695_serial_getc, |
| 110 | .tstc = ks8695_serial_tstc, |
| 111 | }; |
| 112 | |
| 113 | void ks8695_serial_initialize(void) |
| 114 | { |
| 115 | serial_register(&ks8695_serial_drv); |
| 116 | } |
| 117 | |
| 118 | __weak struct serial_device *default_serial_console(void) |
| 119 | { |
| 120 | return &ks8695_serial_drv; |
| 121 | } |