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 = rs_table + CONFIG_PS2SERIAL;
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);
52 int quot = state->baud_base / PS2SER_BAUD;
53 unsigned cval = 0x3; /* 8N1 - 8 data bits, no parity bits, 1 stop bit */
55 /* Set speed, enable interrupts, enable FIFO
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);
64 UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
66 /* If we read 0xff from the LSR, there is no UART here
68 if (ps2ser_in(UART_LSR) == 0xff) {
69 printf ("ps2ser.c: no UART found\n");
73 irq_install_handler(state->irq, ps2ser_interrupt, NULL);
78 void ps2ser_putc(int chr)
81 printf(">>>> 0x%02x\n", chr);
84 while (!(ps2ser_in(UART_LSR) & UART_LSR_THRE));
86 ps2ser_out(UART_TX, chr);
89 static int ps2ser_getc_hw(void)
93 if (ps2ser_in(UART_LSR) & UART_LSR_DR) {
94 res = (ps2ser_in(UART_RX));
100 int ps2ser_getc(void)
109 flags = disable_interrupts();
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);
117 chr = ps2ser_getc_hw();
122 if (flags) enable_interrupts();
125 printf("0x%02x\n", chr);
131 int ps2ser_check(void)
135 flags = disable_interrupts();
136 ps2ser_interrupt(NULL);
137 if (flags) enable_interrupts();
139 return atomic_read(&ps2buf_cnt);
142 static void ps2ser_interrupt(void *dev_id)
148 chr = ps2ser_getc_hw();
149 iir = ps2ser_in(UART_IIR);
150 if (chr < 0) continue;
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);
157 printf ("ps2ser.c: buffer overflow\n");
159 } while (iir & UART_IIR_RDI);
161 if (atomic_read(&ps2buf_cnt)) {
162 ps2mult_callback(atomic_read(&ps2buf_cnt));
166 #endif /* CONFIG_PS2SERIAL */