1 /***********************************************************************
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
8 * Simple 16550A serial driver
10 * Originally from linux source (drivers/char/ps2ser.c)
12 * Used by the PS/2 multiplexer driver (ps2mult.c)
14 ***********************************************************************/
18 #ifdef CONFIG_PS2SERIAL
21 #include <asm/atomic.h>
26 #define PS2SER_BAUD 57600
28 static int ps2ser_getc_hw(void);
29 static void ps2ser_interrupt(void *dev_id);
31 extern struct serial_state rs_table[]; /* in serial.c */
32 static struct serial_state *state;
34 static u_char ps2buf[PS2BUF_SIZE];
35 static atomic_t ps2buf_cnt;
36 static int ps2buf_in_idx;
37 static int ps2buf_out_idx;
40 static inline unsigned int ps2ser_in(int offset)
42 return readb((unsigned long) state->iomem_base + offset);
45 static inline void ps2ser_out(int offset, int value)
47 writeb(value, (unsigned long) state->iomem_base + offset);
55 state = rs_table + CONFIG_PS2SERIAL;
57 quot = state->baud_base / PS2SER_BAUD;
58 cval = 0x3; /* 8N1 - 8 data bits, no parity bits, 1 stop bit */
60 /* Set speed, enable interrupts, enable FIFO
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);
69 UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
71 /* If we read 0xff from the LSR, there is no UART here
73 if (ps2ser_in(UART_LSR) == 0xff) {
74 printf ("ps2ser.c: no UART found\n");
78 irq_install_handler(state->irq, ps2ser_interrupt, NULL);
83 void ps2ser_putc(int chr)
86 printf(">>>> 0x%02x\n", chr);
89 while (!(ps2ser_in(UART_LSR) & UART_LSR_THRE));
91 ps2ser_out(UART_TX, chr);
94 static int ps2ser_getc_hw(void)
98 if (ps2ser_in(UART_LSR) & UART_LSR_DR) {
99 res = (ps2ser_in(UART_RX));
105 int ps2ser_getc(void)
114 flags = disable_interrupts();
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);
122 chr = ps2ser_getc_hw();
127 if (flags) enable_interrupts();
130 printf("0x%02x\n", chr);
136 int ps2ser_check(void)
140 flags = disable_interrupts();
141 ps2ser_interrupt(NULL);
142 if (flags) enable_interrupts();
144 return atomic_read(&ps2buf_cnt);
147 static void ps2ser_interrupt(void *dev_id)
153 chr = ps2ser_getc_hw();
154 iir = ps2ser_in(UART_IIR);
155 if (chr < 0) continue;
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);
162 printf ("ps2ser.c: buffer overflow\n");
164 } while (iir & UART_IIR_RDI);
166 if (atomic_read(&ps2buf_cnt)) {
167 ps2mult_callback(atomic_read(&ps2buf_cnt));
171 #endif /* CONFIG_PS2SERIAL */