ps2ser.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #ifdef CONFIG_MPC5xxx
  27. #if CONFIG_PS2SERIAL == 1
  28. #define PSC_BASE MPC5XXX_PSC1
  29. #elif CONFIG_PS2SERIAL == 2
  30. #define PSC_BASE MPC5XXX_PSC2
  31. #elif CONFIG_PS2SERIAL == 3
  32. #define PSC_BASE MPC5XXX_PSC3
  33. #elif CONFIG_PS2SERIAL == 4
  34. #define PSC_BASE MPC5XXX_PSC4
  35. #elif CONFIG_PS2SERIAL == 5
  36. #define PSC_BASE MPC5XXX_PSC5
  37. #elif CONFIG_PS2SERIAL == 6
  38. #define PSC_BASE MPC5XXX_PSC6
  39. #else
  40. #error CONFIG_PS2SERIAL must be in 1 ... 6
  41. #endif
  42. #else
  43. #if CONFIG_PS2SERIAL == 1
  44. #define COM_BASE (CONFIG_SYS_CCSRBAR+0x4500)
  45. #elif CONFIG_PS2SERIAL == 2
  46. #define COM_BASE (CONFIG_SYS_CCSRBAR+0x4600)
  47. #else
  48. #error CONFIG_PS2SERIAL must be in 1 ... 2
  49. #endif
  50. #endif /* CONFIG_MPC5xxx / other */
  51. static int ps2ser_getc_hw(void);
  52. static void ps2ser_interrupt(void *dev_id);
  53. extern struct serial_state rs_table[]; /* in serial.c */
  54. static u_char ps2buf[PS2BUF_SIZE];
  55. static atomic_t ps2buf_cnt;
  56. static int ps2buf_in_idx;
  57. static int ps2buf_out_idx;
  58. #ifdef CONFIG_MPC5xxx
  59. int ps2ser_init(void)
  60. {
  61. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  62. unsigned long baseclk;
  63. int div;
  64. /* reset PSC */
  65. psc->command = PSC_SEL_MODE_REG_1;
  66. /* select clock sources */
  67. psc->psc_clock_select = 0;
  68. baseclk = (gd->arch.ipb_clk + 16) / 32;
  69. /* switch to UART mode */
  70. psc->sicr = 0;
  71. /* configure parity, bit length and so on */
  72. psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  73. psc->mode = PSC_MODE_ONE_STOP;
  74. /* set up UART divisor */
  75. div = (baseclk + (PS2SER_BAUD/2)) / PS2SER_BAUD;
  76. psc->ctur = (div >> 8) & 0xff;
  77. psc->ctlr = div & 0xff;
  78. /* disable all interrupts */
  79. psc->psc_imr = 0;
  80. /* reset and enable Rx/Tx */
  81. psc->command = PSC_RST_RX;
  82. psc->command = PSC_RST_TX;
  83. psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE;
  84. return (0);
  85. }
  86. #else
  87. int ps2ser_init(void)
  88. {
  89. NS16550_t com_port = (NS16550_t)COM_BASE;
  90. com_port->ier = 0x00;
  91. com_port->lcr = UART_LCR_BKSE | UART_LCR_8N1;
  92. com_port->dll = (CONFIG_SYS_NS16550_CLK / 16 / PS2SER_BAUD) & 0xff;
  93. com_port->dlm = ((CONFIG_SYS_NS16550_CLK / 16 / PS2SER_BAUD) >> 8) & 0xff;
  94. com_port->lcr = UART_LCR_8N1;
  95. com_port->mcr = (UART_MCR_DTR | UART_MCR_RTS);
  96. com_port->fcr = (UART_FCR_FIFO_EN | UART_FCR_RXSR | UART_FCR_TXSR);
  97. return (0);
  98. }
  99. #endif /* CONFIG_MPC5xxx / other */
  100. void ps2ser_putc(int chr)
  101. {
  102. #ifdef CONFIG_MPC5xxx
  103. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  104. #else
  105. NS16550_t com_port = (NS16550_t)COM_BASE;
  106. #endif
  107. debug(">>>> 0x%02x\n", chr);
  108. #ifdef CONFIG_MPC5xxx
  109. while (!(psc->psc_status & PSC_SR_TXRDY));
  110. psc->psc_buffer_8 = chr;
  111. #else
  112. while ((com_port->lsr & UART_LSR_THRE) == 0);
  113. com_port->thr = chr;
  114. #endif
  115. }
  116. static int ps2ser_getc_hw(void)
  117. {
  118. #ifdef CONFIG_MPC5xxx
  119. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  120. #else
  121. NS16550_t com_port = (NS16550_t)COM_BASE;
  122. #endif
  123. int res = -1;
  124. #ifdef CONFIG_MPC5xxx
  125. if (psc->psc_status & PSC_SR_RXRDY) {
  126. res = (psc->psc_buffer_8);
  127. }
  128. #else
  129. if (com_port->lsr & UART_LSR_DR) {
  130. res = com_port->rbr;
  131. }
  132. #endif
  133. return res;
  134. }
  135. int ps2ser_getc(void)
  136. {
  137. volatile int chr;
  138. int flags;
  139. debug("<< ");
  140. flags = disable_interrupts();
  141. do {
  142. if (atomic_read(&ps2buf_cnt) != 0) {
  143. chr = ps2buf[ps2buf_out_idx++];
  144. ps2buf_out_idx &= (PS2BUF_SIZE - 1);
  145. atomic_dec(&ps2buf_cnt);
  146. } else {
  147. chr = ps2ser_getc_hw();
  148. }
  149. }
  150. while (chr < 0);
  151. if (flags)
  152. enable_interrupts();
  153. debug("0x%02x\n", chr);
  154. return chr;
  155. }
  156. int ps2ser_check(void)
  157. {
  158. int flags;
  159. flags = disable_interrupts();
  160. ps2ser_interrupt(NULL);
  161. if (flags) enable_interrupts();
  162. return atomic_read(&ps2buf_cnt);
  163. }
  164. static void ps2ser_interrupt(void *dev_id)
  165. {
  166. #ifdef CONFIG_MPC5xxx
  167. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  168. #else
  169. NS16550_t com_port = (NS16550_t)COM_BASE;
  170. #endif
  171. int chr;
  172. int status;
  173. do {
  174. chr = ps2ser_getc_hw();
  175. #ifdef CONFIG_MPC5xxx
  176. status = psc->psc_status;
  177. #else
  178. status = com_port->lsr;
  179. #endif
  180. if (chr < 0) continue;
  181. if (atomic_read(&ps2buf_cnt) < PS2BUF_SIZE) {
  182. ps2buf[ps2buf_in_idx++] = chr;
  183. ps2buf_in_idx &= (PS2BUF_SIZE - 1);
  184. atomic_inc(&ps2buf_cnt);
  185. } else {
  186. printf ("ps2ser.c: buffer overflow\n");
  187. }
  188. #ifdef CONFIG_MPC5xxx
  189. } while (status & PSC_SR_RXRDY);
  190. #else
  191. } while (status & UART_LSR_DR);
  192. #endif
  193. if (atomic_read(&ps2buf_cnt)) {
  194. ps2mult_callback(atomic_read(&ps2buf_cnt));
  195. }
  196. }