blob: 52f1db9ad9dca60336efe357b191cd6c480b0dc1 [file] [log] [blame]
wdenkb983fa22004-01-16 00:30:56 +00001/***********************************************************************
2 *
3 * (C) Copyright 2004
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
6 * All rights reserved.
7 *
8 * Simple 16550A serial driver
9 *
10 * Originally from linux source (drivers/char/ps2ser.c)
11 *
12 * Used by the PS/2 multiplexer driver (ps2mult.c)
13 *
14 ***********************************************************************/
15
16#include <common.h>
17
18#ifdef CONFIG_PS2SERIAL
19
20#include <asm/io.h>
21#include <asm/atomic.h>
22#include <ps2mult.h>
23
24/* #define DEBUG */
25
26#define PS2SER_BAUD 57600
27
28static int ps2ser_getc_hw(void);
29static void ps2ser_interrupt(void *dev_id);
30
31extern struct serial_state rs_table[]; /* in serial.c */
wdenk398eeff2004-01-21 20:46:28 +000032static struct serial_state *state;
wdenkb983fa22004-01-16 00:30:56 +000033
34static u_char ps2buf[PS2BUF_SIZE];
35static atomic_t ps2buf_cnt;
36static int ps2buf_in_idx;
37static int ps2buf_out_idx;
38
39
40static inline unsigned int ps2ser_in(int offset)
41{
42 return readb((unsigned long) state->iomem_base + offset);
43}
44
45static inline void ps2ser_out(int offset, int value)
46{
47 writeb(value, (unsigned long) state->iomem_base + offset);
48}
49
50int ps2ser_init(void)
51{
wdenk398eeff2004-01-21 20:46:28 +000052 int quot;
53 unsigned cval;
54
55 state = rs_table + CONFIG_PS2SERIAL;
56
57 quot = state->baud_base / PS2SER_BAUD;
58 cval = 0x3; /* 8N1 - 8 data bits, no parity bits, 1 stop bit */
wdenkb983fa22004-01-16 00:30:56 +000059
60 /* Set speed, enable interrupts, enable FIFO
61 */
62 ps2ser_out(UART_LCR, cval | UART_LCR_DLAB);
63 ps2ser_out(UART_DLL, quot & 0xff);
64 ps2ser_out(UART_DLM, quot >> 8);
65 ps2ser_out(UART_LCR, cval);
66 ps2ser_out(UART_IER, UART_IER_RDI);
67 ps2ser_out(UART_MCR, UART_MCR_OUT2 | UART_MCR_DTR | UART_MCR_RTS);
68 ps2ser_out(UART_FCR,
69 UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
70
71 /* If we read 0xff from the LSR, there is no UART here
72 */
73 if (ps2ser_in(UART_LSR) == 0xff) {
74 printf ("ps2ser.c: no UART found\n");
75 return -1;
76 }
77
78 irq_install_handler(state->irq, ps2ser_interrupt, NULL);
79
80 return 0;
81}
82
83void ps2ser_putc(int chr)
84{
85#ifdef DEBUG
86 printf(">>>> 0x%02x\n", chr);
87#endif
88
89 while (!(ps2ser_in(UART_LSR) & UART_LSR_THRE));
90
91 ps2ser_out(UART_TX, chr);
92}
93
94static int ps2ser_getc_hw(void)
95{
96 int res = -1;
97
98 if (ps2ser_in(UART_LSR) & UART_LSR_DR) {
99 res = (ps2ser_in(UART_RX));
100 }
101
102 return res;
103}
104
105int ps2ser_getc(void)
106{
107 volatile int chr;
108 int flags;
109
110#ifdef DEBUG
111 printf("<< ");
112#endif
113
114 flags = disable_interrupts();
115
116 do {
117 if (atomic_read(&ps2buf_cnt) != 0) {
118 chr = ps2buf[ps2buf_out_idx++];
119 ps2buf_out_idx &= (PS2BUF_SIZE - 1);
120 atomic_dec(&ps2buf_cnt);
121 } else {
122 chr = ps2ser_getc_hw();
123 }
124 }
125 while (chr < 0);
126
127 if (flags) enable_interrupts();
128
129#ifdef DEBUG
130 printf("0x%02x\n", chr);
131#endif
132
133 return chr;
134}
135
136int ps2ser_check(void)
137{
138 int flags;
139
140 flags = disable_interrupts();
141 ps2ser_interrupt(NULL);
142 if (flags) enable_interrupts();
143
144 return atomic_read(&ps2buf_cnt);
145}
146
147static void ps2ser_interrupt(void *dev_id)
148{
149 int chr;
150 int iir;
151
152 do {
153 chr = ps2ser_getc_hw();
154 iir = ps2ser_in(UART_IIR);
155 if (chr < 0) continue;
156
157 if (atomic_read(&ps2buf_cnt) < PS2BUF_SIZE) {
158 ps2buf[ps2buf_in_idx++] = chr;
159 ps2buf_in_idx &= (PS2BUF_SIZE - 1);
160 atomic_inc(&ps2buf_cnt);
161 } else {
162 printf ("ps2ser.c: buffer overflow\n");
163 }
164 } while (iir & UART_IIR_RDI);
165
166 if (atomic_read(&ps2buf_cnt)) {
167 ps2mult_callback(atomic_read(&ps2buf_cnt));
168 }
169}
170
171#endif /* CONFIG_PS2SERIAL */