ps2ser.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /***********************************************************************
  2. *
  3. * (C) Copyright 2004-2009
  4. * DENX Software Engineering
  5. * Wolfgang Denk, wd@denx.de
  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. #include <common.h>
  15. #include <asm/io.h>
  16. #include <asm/atomic.h>
  17. #include <ps2mult.h>
  18. /* This is needed for ns16550.h */
  19. #ifndef CONFIG_SYS_NS16550_REG_SIZE
  20. #define CONFIG_SYS_NS16550_REG_SIZE 1
  21. #endif
  22. #include <ns16550.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. /* #define DEBUG */
  25. #define PS2SER_BAUD 57600
  26. #if CONFIG_PS2SERIAL == 1
  27. #define COM_BASE (CONFIG_SYS_CCSRBAR+0x4500)
  28. #elif CONFIG_PS2SERIAL == 2
  29. #define COM_BASE (CONFIG_SYS_CCSRBAR+0x4600)
  30. #else
  31. #error CONFIG_PS2SERIAL must be in 1 ... 2
  32. #endif
  33. static int ps2ser_getc_hw(void);
  34. static void ps2ser_interrupt(void *dev_id);
  35. extern struct serial_state rs_table[]; /* in serial.c */
  36. static u_char ps2buf[PS2BUF_SIZE];
  37. static atomic_t ps2buf_cnt;
  38. static int ps2buf_in_idx;
  39. static int ps2buf_out_idx;
  40. int ps2ser_init(void)
  41. {
  42. NS16550_t com_port = (NS16550_t)COM_BASE;
  43. com_port->ier = 0x00;
  44. com_port->lcr = UART_LCR_BKSE | UART_LCR_8N1;
  45. com_port->dll = (CONFIG_SYS_NS16550_CLK / 16 / PS2SER_BAUD) & 0xff;
  46. com_port->dlm = ((CONFIG_SYS_NS16550_CLK / 16 / PS2SER_BAUD) >> 8) & 0xff;
  47. com_port->lcr = UART_LCR_8N1;
  48. com_port->mcr = (UART_MCR_DTR | UART_MCR_RTS);
  49. com_port->fcr = (UART_FCR_FIFO_EN | UART_FCR_RXSR | UART_FCR_TXSR);
  50. return (0);
  51. }
  52. void ps2ser_putc(int chr)
  53. {
  54. NS16550_t com_port = (NS16550_t)COM_BASE;
  55. debug(">>>> 0x%02x\n", chr);
  56. while ((com_port->lsr & UART_LSR_THRE) == 0);
  57. com_port->thr = chr;
  58. }
  59. static int ps2ser_getc_hw(void)
  60. {
  61. NS16550_t com_port = (NS16550_t)COM_BASE;
  62. int res = -1;
  63. if (com_port->lsr & UART_LSR_DR) {
  64. res = com_port->rbr;
  65. }
  66. return res;
  67. }
  68. int ps2ser_getc(void)
  69. {
  70. volatile int chr;
  71. int flags;
  72. debug("<< ");
  73. flags = disable_interrupts();
  74. do {
  75. if (atomic_read(&ps2buf_cnt) != 0) {
  76. chr = ps2buf[ps2buf_out_idx++];
  77. ps2buf_out_idx &= (PS2BUF_SIZE - 1);
  78. atomic_dec(&ps2buf_cnt);
  79. } else {
  80. chr = ps2ser_getc_hw();
  81. }
  82. }
  83. while (chr < 0);
  84. if (flags)
  85. enable_interrupts();
  86. debug("0x%02x\n", chr);
  87. return chr;
  88. }
  89. int ps2ser_check(void)
  90. {
  91. int flags;
  92. flags = disable_interrupts();
  93. ps2ser_interrupt(NULL);
  94. if (flags) enable_interrupts();
  95. return atomic_read(&ps2buf_cnt);
  96. }
  97. static void ps2ser_interrupt(void *dev_id)
  98. {
  99. NS16550_t com_port = (NS16550_t)COM_BASE;
  100. int chr;
  101. int status;
  102. do {
  103. chr = ps2ser_getc_hw();
  104. status = com_port->lsr;
  105. if (chr < 0) continue;
  106. if (atomic_read(&ps2buf_cnt) < PS2BUF_SIZE) {
  107. ps2buf[ps2buf_in_idx++] = chr;
  108. ps2buf_in_idx &= (PS2BUF_SIZE - 1);
  109. atomic_inc(&ps2buf_cnt);
  110. } else {
  111. printf ("ps2ser.c: buffer overflow\n");
  112. }
  113. } while (status & UART_LSR_DR);
  114. if (atomic_read(&ps2buf_cnt)) {
  115. ps2mult_callback(atomic_read(&ps2buf_cnt));
  116. }
  117. }