blob: 0b5ce06853e4101a0e1a28b7ba13ac9f4847dd19 [file] [log] [blame]
wdenk1c437712004-01-16 00:30:56 +00001/***********************************************************************
2 *
Detlev Zundel200779e2009-04-03 11:53:01 +02003 * (C) Copyright 2004-2009
wdenk1c437712004-01-16 00:30:56 +00004 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
wdenk1c437712004-01-16 00:30:56 +00006 *
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
wdenk1c437712004-01-16 00:30:56 +000017#include <asm/io.h>
18#include <asm/atomic.h>
19#include <ps2mult.h>
Detlev Zundel200779e2009-04-03 11:53:01 +020020/* This is needed for ns16550.h */
21#ifndef CONFIG_SYS_NS16550_REG_SIZE
22#define CONFIG_SYS_NS16550_REG_SIZE 1
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020023#endif
Detlev Zundel200779e2009-04-03 11:53:01 +020024#include <ns16550.h>
wdenk1c437712004-01-16 00:30:56 +000025
Wolfgang Denkd87080b2006-03-31 18:32:53 +020026DECLARE_GLOBAL_DATA_PTR;
27
wdenk1c437712004-01-16 00:30:56 +000028/* #define DEBUG */
29
30#define PS2SER_BAUD 57600
31
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020032#if CONFIG_PS2SERIAL == 1
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020033#define COM_BASE (CONFIG_SYS_CCSRBAR+0x4500)
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020034#elif CONFIG_PS2SERIAL == 2
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020035#define COM_BASE (CONFIG_SYS_CCSRBAR+0x4600)
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020036#else
37#error CONFIG_PS2SERIAL must be in 1 ... 2
38#endif
39
wdenk1c437712004-01-16 00:30:56 +000040static int ps2ser_getc_hw(void);
41static void ps2ser_interrupt(void *dev_id);
42
43extern struct serial_state rs_table[]; /* in serial.c */
wdenk1c437712004-01-16 00:30:56 +000044
45static u_char ps2buf[PS2BUF_SIZE];
46static atomic_t ps2buf_cnt;
47static int ps2buf_in_idx;
48static int ps2buf_out_idx;
49
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020050int ps2ser_init(void)
51{
52 NS16550_t com_port = (NS16550_t)COM_BASE;
53
54 com_port->ier = 0x00;
Detlev Zundel200779e2009-04-03 11:53:01 +020055 com_port->lcr = UART_LCR_BKSE | UART_LCR_8N1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020056 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 Zundel200779e2009-04-03 11:53:01 +020058 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 Denkbc8bb6d2006-06-16 16:40:54 +020061
62 return (0);
63}
64
wdenk1c437712004-01-16 00:30:56 +000065void ps2ser_putc(int chr)
66{
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020067 NS16550_t com_port = (NS16550_t)COM_BASE;
Wolfgang Denk77efe352010-09-19 21:28:25 +020068 debug(">>>> 0x%02x\n", chr);
wdenk1c437712004-01-16 00:30:56 +000069
Detlev Zundel200779e2009-04-03 11:53:01 +020070 while ((com_port->lsr & UART_LSR_THRE) == 0);
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020071 com_port->thr = chr;
wdenk1c437712004-01-16 00:30:56 +000072}
73
74static int ps2ser_getc_hw(void)
75{
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020076 NS16550_t com_port = (NS16550_t)COM_BASE;
wdenk1c437712004-01-16 00:30:56 +000077 int res = -1;
78
Detlev Zundel200779e2009-04-03 11:53:01 +020079 if (com_port->lsr & UART_LSR_DR) {
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +020080 res = com_port->rbr;
81 }
wdenk1c437712004-01-16 00:30:56 +000082
83 return res;
84}
85
86int ps2ser_getc(void)
87{
88 volatile int chr;
89 int flags;
90
Wolfgang Denk77efe352010-09-19 21:28:25 +020091 debug("<< ");
wdenk1c437712004-01-16 00:30:56 +000092
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 Denk77efe352010-09-19 21:28:25 +0200106 if (flags)
107 enable_interrupts();
wdenk1c437712004-01-16 00:30:56 +0000108
Wolfgang Denk77efe352010-09-19 21:28:25 +0200109 debug("0x%02x\n", chr);
wdenk1c437712004-01-16 00:30:56 +0000110
111 return chr;
112}
113
114int 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
125static void ps2ser_interrupt(void *dev_id)
126{
Wolfgang Denkbc8bb6d2006-06-16 16:40:54 +0200127 NS16550_t com_port = (NS16550_t)COM_BASE;
wdenk1c437712004-01-16 00:30:56 +0000128 int chr;
wdenk7e6bf352004-12-12 22:06:17 +0000129 int status;
wdenk1c437712004-01-16 00:30:56 +0000130
131 do {
132 chr = ps2ser_getc_hw();
Wolfgang Denk77efe352010-09-19 21:28:25 +0200133 status = com_port->lsr;
wdenk1c437712004-01-16 00:30:56 +0000134 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 Denk77efe352010-09-19 21:28:25 +0200143 } while (status & UART_LSR_DR);
wdenk1c437712004-01-16 00:30:56 +0000144 if (atomic_read(&ps2buf_cnt)) {
145 ps2mult_callback(atomic_read(&ps2buf_cnt));
146 }
147}