wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 1 | /*********************************************************************** |
| 2 | * |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 3 | * (C) Copyright 2004-2009 |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 4 | * DENX Software Engineering |
| 5 | * Wolfgang Denk, wd@denx.de |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 6 | * |
| 7 | * Simple 16550A serial driver |
| 8 | * |
| 9 | * Originally from linux source (drivers/char/ps2ser.c) |
| 10 | * |
| 11 | * Used by the PS/2 multiplexer driver (ps2mult.c) |
| 12 | * |
| 13 | ***********************************************************************/ |
| 14 | |
| 15 | #include <common.h> |
| 16 | |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/atomic.h> |
| 19 | #include <ps2mult.h> |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 20 | /* This is needed for ns16550.h */ |
| 21 | #ifndef CONFIG_SYS_NS16550_REG_SIZE |
| 22 | #define CONFIG_SYS_NS16550_REG_SIZE 1 |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 23 | #endif |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 24 | #include <ns16550.h> |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 25 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 28 | /* #define DEBUG */ |
| 29 | |
| 30 | #define PS2SER_BAUD 57600 |
| 31 | |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 32 | #if CONFIG_PS2SERIAL == 1 |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 33 | #define COM_BASE (CONFIG_SYS_CCSRBAR+0x4500) |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 34 | #elif CONFIG_PS2SERIAL == 2 |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 35 | #define COM_BASE (CONFIG_SYS_CCSRBAR+0x4600) |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 36 | #else |
| 37 | #error CONFIG_PS2SERIAL must be in 1 ... 2 |
| 38 | #endif |
| 39 | |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 40 | static int ps2ser_getc_hw(void); |
| 41 | static void ps2ser_interrupt(void *dev_id); |
| 42 | |
| 43 | extern struct serial_state rs_table[]; /* in serial.c */ |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 44 | |
| 45 | static u_char ps2buf[PS2BUF_SIZE]; |
| 46 | static atomic_t ps2buf_cnt; |
| 47 | static int ps2buf_in_idx; |
| 48 | static int ps2buf_out_idx; |
| 49 | |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 50 | int ps2ser_init(void) |
| 51 | { |
| 52 | NS16550_t com_port = (NS16550_t)COM_BASE; |
| 53 | |
| 54 | com_port->ier = 0x00; |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 55 | com_port->lcr = UART_LCR_BKSE | UART_LCR_8N1; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 56 | com_port->dll = (CONFIG_SYS_NS16550_CLK / 16 / PS2SER_BAUD) & 0xff; |
| 57 | com_port->dlm = ((CONFIG_SYS_NS16550_CLK / 16 / PS2SER_BAUD) >> 8) & 0xff; |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 58 | com_port->lcr = UART_LCR_8N1; |
| 59 | com_port->mcr = (UART_MCR_DTR | UART_MCR_RTS); |
| 60 | com_port->fcr = (UART_FCR_FIFO_EN | UART_FCR_RXSR | UART_FCR_TXSR); |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 61 | |
| 62 | return (0); |
| 63 | } |
| 64 | |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 65 | void ps2ser_putc(int chr) |
| 66 | { |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 67 | NS16550_t com_port = (NS16550_t)COM_BASE; |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 68 | debug(">>>> 0x%02x\n", chr); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 69 | |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 70 | while ((com_port->lsr & UART_LSR_THRE) == 0); |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 71 | com_port->thr = chr; |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static int ps2ser_getc_hw(void) |
| 75 | { |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 76 | NS16550_t com_port = (NS16550_t)COM_BASE; |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 77 | int res = -1; |
| 78 | |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 79 | if (com_port->lsr & UART_LSR_DR) { |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 80 | res = com_port->rbr; |
| 81 | } |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 82 | |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | int ps2ser_getc(void) |
| 87 | { |
| 88 | volatile int chr; |
| 89 | int flags; |
| 90 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 91 | debug("<< "); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 92 | |
| 93 | flags = disable_interrupts(); |
| 94 | |
| 95 | do { |
| 96 | if (atomic_read(&ps2buf_cnt) != 0) { |
| 97 | chr = ps2buf[ps2buf_out_idx++]; |
| 98 | ps2buf_out_idx &= (PS2BUF_SIZE - 1); |
| 99 | atomic_dec(&ps2buf_cnt); |
| 100 | } else { |
| 101 | chr = ps2ser_getc_hw(); |
| 102 | } |
| 103 | } |
| 104 | while (chr < 0); |
| 105 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 106 | if (flags) |
| 107 | enable_interrupts(); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 108 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 109 | debug("0x%02x\n", chr); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 110 | |
| 111 | return chr; |
| 112 | } |
| 113 | |
| 114 | int ps2ser_check(void) |
| 115 | { |
| 116 | int flags; |
| 117 | |
| 118 | flags = disable_interrupts(); |
| 119 | ps2ser_interrupt(NULL); |
| 120 | if (flags) enable_interrupts(); |
| 121 | |
| 122 | return atomic_read(&ps2buf_cnt); |
| 123 | } |
| 124 | |
| 125 | static void ps2ser_interrupt(void *dev_id) |
| 126 | { |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 127 | NS16550_t com_port = (NS16550_t)COM_BASE; |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 128 | int chr; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 129 | int status; |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 130 | |
| 131 | do { |
| 132 | chr = ps2ser_getc_hw(); |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 133 | status = com_port->lsr; |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 134 | if (chr < 0) continue; |
| 135 | |
| 136 | if (atomic_read(&ps2buf_cnt) < PS2BUF_SIZE) { |
| 137 | ps2buf[ps2buf_in_idx++] = chr; |
| 138 | ps2buf_in_idx &= (PS2BUF_SIZE - 1); |
| 139 | atomic_inc(&ps2buf_cnt); |
| 140 | } else { |
| 141 | printf ("ps2ser.c: buffer overflow\n"); |
| 142 | } |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 143 | } while (status & UART_LSR_DR); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 144 | if (atomic_read(&ps2buf_cnt)) { |
| 145 | ps2mult_callback(atomic_read(&ps2buf_cnt)); |
| 146 | } |
| 147 | } |