wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 1 | /*********************************************************************** |
| 2 | * |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 3 | * (C) Copyright 2004-2009 |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 4 | * DENX Software Engineering |
| 5 | * Wolfgang Denk, wd@denx.de |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 6 | * |
| 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 | |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/atomic.h> |
| 19 | #include <ps2mult.h> |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 20 | /* This is needed for ns16550.h */ |
| 21 | #ifndef CONFIG_SYS_NS16550_REG_SIZE |
| 22 | #define CONFIG_SYS_NS16550_REG_SIZE 1 |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 23 | #endif |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 24 | #include <ns16550.h> |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 25 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 28 | /* #define DEBUG */ |
| 29 | |
| 30 | #define PS2SER_BAUD 57600 |
| 31 | |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 32 | #ifdef CONFIG_MPC5xxx |
| 33 | #if CONFIG_PS2SERIAL == 1 |
| 34 | #define PSC_BASE MPC5XXX_PSC1 |
| 35 | #elif CONFIG_PS2SERIAL == 2 |
| 36 | #define PSC_BASE MPC5XXX_PSC2 |
| 37 | #elif CONFIG_PS2SERIAL == 3 |
| 38 | #define PSC_BASE MPC5XXX_PSC3 |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 39 | #elif CONFIG_PS2SERIAL == 4 |
| 40 | #define PSC_BASE MPC5XXX_PSC4 |
| 41 | #elif CONFIG_PS2SERIAL == 5 |
| 42 | #define PSC_BASE MPC5XXX_PSC5 |
| 43 | #elif CONFIG_PS2SERIAL == 6 |
| 44 | #define PSC_BASE MPC5XXX_PSC6 |
| 45 | #else |
| 46 | #error CONFIG_PS2SERIAL must be in 1 ... 6 |
| 47 | #endif |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 48 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 49 | #else |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 50 | |
| 51 | #if CONFIG_PS2SERIAL == 1 |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 52 | #define COM_BASE (CONFIG_SYS_CCSRBAR+0x4500) |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 53 | #elif CONFIG_PS2SERIAL == 2 |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 54 | #define COM_BASE (CONFIG_SYS_CCSRBAR+0x4600) |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 55 | #else |
| 56 | #error CONFIG_PS2SERIAL must be in 1 ... 2 |
| 57 | #endif |
| 58 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 59 | #endif /* CONFIG_MPC5xxx / other */ |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 60 | |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 61 | static int ps2ser_getc_hw(void); |
| 62 | static void ps2ser_interrupt(void *dev_id); |
| 63 | |
| 64 | extern struct serial_state rs_table[]; /* in serial.c */ |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 65 | |
| 66 | static u_char ps2buf[PS2BUF_SIZE]; |
| 67 | static atomic_t ps2buf_cnt; |
| 68 | static int ps2buf_in_idx; |
| 69 | static int ps2buf_out_idx; |
| 70 | |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 71 | #ifdef CONFIG_MPC5xxx |
| 72 | int ps2ser_init(void) |
| 73 | { |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 74 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
| 75 | unsigned long baseclk; |
| 76 | int div; |
| 77 | |
| 78 | /* reset PSC */ |
| 79 | psc->command = PSC_SEL_MODE_REG_1; |
| 80 | |
| 81 | /* select clock sources */ |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 82 | psc->psc_clock_select = 0; |
Simon Glass | 4f8c5f0 | 2012-12-13 20:48:53 +0000 | [diff] [blame] | 83 | baseclk = (gd->arch.ipb_clk + 16) / 32; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 84 | |
| 85 | /* switch to UART mode */ |
| 86 | psc->sicr = 0; |
| 87 | |
| 88 | /* configure parity, bit length and so on */ |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 89 | psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 90 | psc->mode = PSC_MODE_ONE_STOP; |
| 91 | |
| 92 | /* set up UART divisor */ |
| 93 | div = (baseclk + (PS2SER_BAUD/2)) / PS2SER_BAUD; |
| 94 | psc->ctur = (div >> 8) & 0xff; |
| 95 | psc->ctlr = div & 0xff; |
| 96 | |
| 97 | /* disable all interrupts */ |
| 98 | psc->psc_imr = 0; |
| 99 | |
| 100 | /* reset and enable Rx/Tx */ |
| 101 | psc->command = PSC_RST_RX; |
| 102 | psc->command = PSC_RST_TX; |
| 103 | psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE; |
| 104 | |
| 105 | return (0); |
| 106 | } |
| 107 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 108 | #else |
| 109 | |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 110 | int ps2ser_init(void) |
| 111 | { |
| 112 | NS16550_t com_port = (NS16550_t)COM_BASE; |
| 113 | |
| 114 | com_port->ier = 0x00; |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 115 | com_port->lcr = UART_LCR_BKSE | UART_LCR_8N1; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 116 | com_port->dll = (CONFIG_SYS_NS16550_CLK / 16 / PS2SER_BAUD) & 0xff; |
| 117 | com_port->dlm = ((CONFIG_SYS_NS16550_CLK / 16 / PS2SER_BAUD) >> 8) & 0xff; |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 118 | com_port->lcr = UART_LCR_8N1; |
| 119 | com_port->mcr = (UART_MCR_DTR | UART_MCR_RTS); |
| 120 | com_port->fcr = (UART_FCR_FIFO_EN | UART_FCR_RXSR | UART_FCR_TXSR); |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 121 | |
| 122 | return (0); |
| 123 | } |
| 124 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 125 | #endif /* CONFIG_MPC5xxx / other */ |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 126 | |
| 127 | void ps2ser_putc(int chr) |
| 128 | { |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 129 | #ifdef CONFIG_MPC5xxx |
| 130 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 131 | #else |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 132 | NS16550_t com_port = (NS16550_t)COM_BASE; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 133 | #endif |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 134 | debug(">>>> 0x%02x\n", chr); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 135 | |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 136 | #ifdef CONFIG_MPC5xxx |
| 137 | while (!(psc->psc_status & PSC_SR_TXRDY)); |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 138 | |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 139 | psc->psc_buffer_8 = chr; |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 140 | #else |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 141 | while ((com_port->lsr & UART_LSR_THRE) == 0); |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 142 | com_port->thr = chr; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 143 | #endif |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int ps2ser_getc_hw(void) |
| 147 | { |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 148 | #ifdef CONFIG_MPC5xxx |
| 149 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 150 | #else |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 151 | NS16550_t com_port = (NS16550_t)COM_BASE; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 152 | #endif |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 153 | int res = -1; |
| 154 | |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 155 | #ifdef CONFIG_MPC5xxx |
| 156 | if (psc->psc_status & PSC_SR_RXRDY) { |
| 157 | res = (psc->psc_buffer_8); |
| 158 | } |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 159 | #else |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 160 | if (com_port->lsr & UART_LSR_DR) { |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 161 | res = com_port->rbr; |
| 162 | } |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 163 | #endif |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 164 | |
| 165 | return res; |
| 166 | } |
| 167 | |
| 168 | int ps2ser_getc(void) |
| 169 | { |
| 170 | volatile int chr; |
| 171 | int flags; |
| 172 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 173 | debug("<< "); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 174 | |
| 175 | flags = disable_interrupts(); |
| 176 | |
| 177 | do { |
| 178 | if (atomic_read(&ps2buf_cnt) != 0) { |
| 179 | chr = ps2buf[ps2buf_out_idx++]; |
| 180 | ps2buf_out_idx &= (PS2BUF_SIZE - 1); |
| 181 | atomic_dec(&ps2buf_cnt); |
| 182 | } else { |
| 183 | chr = ps2ser_getc_hw(); |
| 184 | } |
| 185 | } |
| 186 | while (chr < 0); |
| 187 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 188 | if (flags) |
| 189 | enable_interrupts(); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 190 | |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 191 | debug("0x%02x\n", chr); |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 192 | |
| 193 | return chr; |
| 194 | } |
| 195 | |
| 196 | int ps2ser_check(void) |
| 197 | { |
| 198 | int flags; |
| 199 | |
| 200 | flags = disable_interrupts(); |
| 201 | ps2ser_interrupt(NULL); |
| 202 | if (flags) enable_interrupts(); |
| 203 | |
| 204 | return atomic_read(&ps2buf_cnt); |
| 205 | } |
| 206 | |
| 207 | static void ps2ser_interrupt(void *dev_id) |
| 208 | { |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 209 | #ifdef CONFIG_MPC5xxx |
| 210 | volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE; |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 211 | #else |
Wolfgang Denk | 1216885 | 2006-06-16 16:40:54 +0200 | [diff] [blame] | 212 | NS16550_t com_port = (NS16550_t)COM_BASE; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 213 | #endif |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 214 | int chr; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 215 | int status; |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 216 | |
| 217 | do { |
| 218 | chr = ps2ser_getc_hw(); |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 219 | #ifdef CONFIG_MPC5xxx |
| 220 | status = psc->psc_status; |
| 221 | #else |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 222 | status = com_port->lsr; |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 223 | #endif |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 224 | if (chr < 0) continue; |
| 225 | |
| 226 | if (atomic_read(&ps2buf_cnt) < PS2BUF_SIZE) { |
| 227 | ps2buf[ps2buf_in_idx++] = chr; |
| 228 | ps2buf_in_idx &= (PS2BUF_SIZE - 1); |
| 229 | atomic_inc(&ps2buf_cnt); |
| 230 | } else { |
| 231 | printf ("ps2ser.c: buffer overflow\n"); |
| 232 | } |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 233 | #ifdef CONFIG_MPC5xxx |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 234 | } while (status & PSC_SR_RXRDY); |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 235 | #else |
Wolfgang Denk | fb52e95 | 2010-09-19 21:28:25 +0200 | [diff] [blame] | 236 | } while (status & UART_LSR_DR); |
wdenk | dc13044 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 237 | #endif |
wdenk | b983fa2 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 238 | if (atomic_read(&ps2buf_cnt)) { |
| 239 | ps2mult_callback(atomic_read(&ps2buf_cnt)); |
| 240 | } |
| 241 | } |