blob: 71658d7ca0a9c8649fa0e98c52a7d9c11fcaab0c [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 */
32static struct serial_state *state = rs_table + CONFIG_PS2SERIAL;
33
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{
52 int quot = state->baud_base / PS2SER_BAUD;
53 unsigned cval = 0x3; /* 8N1 - 8 data bits, no parity bits, 1 stop bit */
54
55 /* Set speed, enable interrupts, enable FIFO
56 */
57 ps2ser_out(UART_LCR, cval | UART_LCR_DLAB);
58 ps2ser_out(UART_DLL, quot & 0xff);
59 ps2ser_out(UART_DLM, quot >> 8);
60 ps2ser_out(UART_LCR, cval);
61 ps2ser_out(UART_IER, UART_IER_RDI);
62 ps2ser_out(UART_MCR, UART_MCR_OUT2 | UART_MCR_DTR | UART_MCR_RTS);
63 ps2ser_out(UART_FCR,
64 UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
65
66 /* If we read 0xff from the LSR, there is no UART here
67 */
68 if (ps2ser_in(UART_LSR) == 0xff) {
69 printf ("ps2ser.c: no UART found\n");
70 return -1;
71 }
72
73 irq_install_handler(state->irq, ps2ser_interrupt, NULL);
74
75 return 0;
76}
77
78void ps2ser_putc(int chr)
79{
80#ifdef DEBUG
81 printf(">>>> 0x%02x\n", chr);
82#endif
83
84 while (!(ps2ser_in(UART_LSR) & UART_LSR_THRE));
85
86 ps2ser_out(UART_TX, chr);
87}
88
89static int ps2ser_getc_hw(void)
90{
91 int res = -1;
92
93 if (ps2ser_in(UART_LSR) & UART_LSR_DR) {
94 res = (ps2ser_in(UART_RX));
95 }
96
97 return res;
98}
99
100int ps2ser_getc(void)
101{
102 volatile int chr;
103 int flags;
104
105#ifdef DEBUG
106 printf("<< ");
107#endif
108
109 flags = disable_interrupts();
110
111 do {
112 if (atomic_read(&ps2buf_cnt) != 0) {
113 chr = ps2buf[ps2buf_out_idx++];
114 ps2buf_out_idx &= (PS2BUF_SIZE - 1);
115 atomic_dec(&ps2buf_cnt);
116 } else {
117 chr = ps2ser_getc_hw();
118 }
119 }
120 while (chr < 0);
121
122 if (flags) enable_interrupts();
123
124#ifdef DEBUG
125 printf("0x%02x\n", chr);
126#endif
127
128 return chr;
129}
130
131int ps2ser_check(void)
132{
133 int flags;
134
135 flags = disable_interrupts();
136 ps2ser_interrupt(NULL);
137 if (flags) enable_interrupts();
138
139 return atomic_read(&ps2buf_cnt);
140}
141
142static void ps2ser_interrupt(void *dev_id)
143{
144 int chr;
145 int iir;
146
147 do {
148 chr = ps2ser_getc_hw();
149 iir = ps2ser_in(UART_IIR);
150 if (chr < 0) continue;
151
152 if (atomic_read(&ps2buf_cnt) < PS2BUF_SIZE) {
153 ps2buf[ps2buf_in_idx++] = chr;
154 ps2buf_in_idx &= (PS2BUF_SIZE - 1);
155 atomic_inc(&ps2buf_cnt);
156 } else {
157 printf ("ps2ser.c: buffer overflow\n");
158 }
159 } while (iir & UART_IIR_RDI);
160
161 if (atomic_read(&ps2buf_cnt)) {
162 ps2mult_callback(atomic_read(&ps2buf_cnt));
163 }
164}
165
166#endif /* CONFIG_PS2SERIAL */