ps2ser.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /***********************************************************************
  2. *
  3. * (C) Copyright 2004
  4. * DENX Software Engineering
  5. * Wolfgang Denk, wd@denx.de
  6. * All rights reserved.
  7. *
  8. * Simple 16550A serial driver
  9. *
  10. * Originally from linux source (drivers/char/ps2ser.c)
  11. *
  12. * Used by the PS/2 multiplexer driver (ps2mult.c)
  13. *
  14. ***********************************************************************/
  15. #include <common.h>
  16. #ifdef CONFIG_PS2SERIAL
  17. #include <asm/io.h>
  18. #include <asm/atomic.h>
  19. #include <ps2mult.h>
  20. #if defined(CFG_NS16550) || defined(CONFIG_MPC85xx)
  21. #include <ns16550.h>
  22. #endif
  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 defined(CONFIG_MGT5100)
  34. #error CONFIG_PS2SERIAL must be in 1, 2 or 3
  35. #elif CONFIG_PS2SERIAL == 4
  36. #define PSC_BASE MPC5XXX_PSC4
  37. #elif CONFIG_PS2SERIAL == 5
  38. #define PSC_BASE MPC5XXX_PSC5
  39. #elif CONFIG_PS2SERIAL == 6
  40. #define PSC_BASE MPC5XXX_PSC6
  41. #else
  42. #error CONFIG_PS2SERIAL must be in 1 ... 6
  43. #endif
  44. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  45. #if CONFIG_PS2SERIAL == 1
  46. #define COM_BASE (CFG_CCSRBAR+0x4500)
  47. #elif CONFIG_PS2SERIAL == 2
  48. #define COM_BASE (CFG_CCSRBAR+0x4600)
  49. #else
  50. #error CONFIG_PS2SERIAL must be in 1 ... 2
  51. #endif
  52. #endif /* CONFIG_MPC5xxx / CONFIG_MPC8540 / other */
  53. static int ps2ser_getc_hw(void);
  54. static void ps2ser_interrupt(void *dev_id);
  55. extern struct serial_state rs_table[]; /* in serial.c */
  56. #if !defined(CONFIG_MPC5xxx) && !defined(CONFIG_MPC8540) && !defined(CONFIG_MPC8541) && !defined(CONFIG_MPC8555)
  57. static struct serial_state *state;
  58. #endif
  59. static u_char ps2buf[PS2BUF_SIZE];
  60. static atomic_t ps2buf_cnt;
  61. static int ps2buf_in_idx;
  62. static int ps2buf_out_idx;
  63. #ifdef CONFIG_MPC5xxx
  64. int ps2ser_init(void)
  65. {
  66. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  67. unsigned long baseclk;
  68. int div;
  69. /* reset PSC */
  70. psc->command = PSC_SEL_MODE_REG_1;
  71. /* select clock sources */
  72. #if defined(CONFIG_MGT5100)
  73. psc->psc_clock_select = 0xdd00;
  74. baseclk = (CFG_MPC5XXX_CLKIN + 16) / 32;
  75. #elif defined(CONFIG_MPC5200)
  76. psc->psc_clock_select = 0;
  77. baseclk = (gd->ipb_clk + 16) / 32;
  78. #endif
  79. /* switch to UART mode */
  80. psc->sicr = 0;
  81. /* configure parity, bit length and so on */
  82. #if defined(CONFIG_MGT5100)
  83. psc->mode = PSC_MODE_ERR | PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  84. #elif defined(CONFIG_MPC5200)
  85. psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE;
  86. #endif
  87. psc->mode = PSC_MODE_ONE_STOP;
  88. /* set up UART divisor */
  89. div = (baseclk + (PS2SER_BAUD/2)) / PS2SER_BAUD;
  90. psc->ctur = (div >> 8) & 0xff;
  91. psc->ctlr = div & 0xff;
  92. /* disable all interrupts */
  93. psc->psc_imr = 0;
  94. /* reset and enable Rx/Tx */
  95. psc->command = PSC_RST_RX;
  96. psc->command = PSC_RST_TX;
  97. psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE;
  98. return (0);
  99. }
  100. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  101. int ps2ser_init(void)
  102. {
  103. NS16550_t com_port = (NS16550_t)COM_BASE;
  104. com_port->ier = 0x00;
  105. com_port->lcr = LCR_BKSE | LCR_8N1;
  106. com_port->dll = (CFG_NS16550_CLK / 16 / PS2SER_BAUD) & 0xff;
  107. com_port->dlm = ((CFG_NS16550_CLK / 16 / PS2SER_BAUD) >> 8) & 0xff;
  108. com_port->lcr = LCR_8N1;
  109. com_port->mcr = (MCR_DTR | MCR_RTS);
  110. com_port->fcr = (FCR_FIFO_EN | FCR_RXSR | FCR_TXSR);
  111. return (0);
  112. }
  113. #else /* !CONFIG_MPC5xxx && !CONFIG_MPC8540 / other */
  114. static inline unsigned int ps2ser_in(int offset)
  115. {
  116. return readb((unsigned long) state->iomem_base + offset);
  117. }
  118. static inline void ps2ser_out(int offset, int value)
  119. {
  120. writeb(value, (unsigned long) state->iomem_base + offset);
  121. }
  122. int ps2ser_init(void)
  123. {
  124. int quot;
  125. unsigned cval;
  126. state = rs_table + CONFIG_PS2SERIAL;
  127. quot = state->baud_base / PS2SER_BAUD;
  128. cval = 0x3; /* 8N1 - 8 data bits, no parity bits, 1 stop bit */
  129. /* Set speed, enable interrupts, enable FIFO
  130. */
  131. ps2ser_out(UART_LCR, cval | UART_LCR_DLAB);
  132. ps2ser_out(UART_DLL, quot & 0xff);
  133. ps2ser_out(UART_DLM, quot >> 8);
  134. ps2ser_out(UART_LCR, cval);
  135. ps2ser_out(UART_IER, UART_IER_RDI);
  136. ps2ser_out(UART_MCR, UART_MCR_OUT2 | UART_MCR_DTR | UART_MCR_RTS);
  137. ps2ser_out(UART_FCR,
  138. UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  139. /* If we read 0xff from the LSR, there is no UART here
  140. */
  141. if (ps2ser_in(UART_LSR) == 0xff) {
  142. printf ("ps2ser.c: no UART found\n");
  143. return -1;
  144. }
  145. irq_install_handler(state->irq, ps2ser_interrupt, NULL);
  146. return 0;
  147. }
  148. #endif /* CONFIG_MPC5xxx / CONFIG_MPC8540 / other */
  149. void ps2ser_putc(int chr)
  150. {
  151. #ifdef CONFIG_MPC5xxx
  152. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  153. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  154. NS16550_t com_port = (NS16550_t)COM_BASE;
  155. #endif
  156. #ifdef DEBUG
  157. printf(">>>> 0x%02x\n", chr);
  158. #endif
  159. #ifdef CONFIG_MPC5xxx
  160. while (!(psc->psc_status & PSC_SR_TXRDY));
  161. psc->psc_buffer_8 = chr;
  162. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  163. while ((com_port->lsr & LSR_THRE) == 0);
  164. com_port->thr = chr;
  165. #else
  166. while (!(ps2ser_in(UART_LSR) & UART_LSR_THRE));
  167. ps2ser_out(UART_TX, chr);
  168. #endif
  169. }
  170. static int ps2ser_getc_hw(void)
  171. {
  172. #ifdef CONFIG_MPC5xxx
  173. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  174. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  175. NS16550_t com_port = (NS16550_t)COM_BASE;
  176. #endif
  177. int res = -1;
  178. #ifdef CONFIG_MPC5xxx
  179. if (psc->psc_status & PSC_SR_RXRDY) {
  180. res = (psc->psc_buffer_8);
  181. }
  182. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  183. if (com_port->lsr & LSR_DR) {
  184. res = com_port->rbr;
  185. }
  186. #else
  187. if (ps2ser_in(UART_LSR) & UART_LSR_DR) {
  188. res = (ps2ser_in(UART_RX));
  189. }
  190. #endif
  191. return res;
  192. }
  193. int ps2ser_getc(void)
  194. {
  195. volatile int chr;
  196. int flags;
  197. #ifdef DEBUG
  198. printf("<< ");
  199. #endif
  200. flags = disable_interrupts();
  201. do {
  202. if (atomic_read(&ps2buf_cnt) != 0) {
  203. chr = ps2buf[ps2buf_out_idx++];
  204. ps2buf_out_idx &= (PS2BUF_SIZE - 1);
  205. atomic_dec(&ps2buf_cnt);
  206. } else {
  207. chr = ps2ser_getc_hw();
  208. }
  209. }
  210. while (chr < 0);
  211. if (flags) enable_interrupts();
  212. #ifdef DEBUG
  213. printf("0x%02x\n", chr);
  214. #endif
  215. return chr;
  216. }
  217. int ps2ser_check(void)
  218. {
  219. int flags;
  220. flags = disable_interrupts();
  221. ps2ser_interrupt(NULL);
  222. if (flags) enable_interrupts();
  223. return atomic_read(&ps2buf_cnt);
  224. }
  225. static void ps2ser_interrupt(void *dev_id)
  226. {
  227. #ifdef CONFIG_MPC5xxx
  228. volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
  229. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  230. NS16550_t com_port = (NS16550_t)COM_BASE;
  231. #endif
  232. int chr;
  233. int status;
  234. do {
  235. chr = ps2ser_getc_hw();
  236. #ifdef CONFIG_MPC5xxx
  237. status = psc->psc_status;
  238. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  239. status = com_port->lsr;
  240. #else
  241. status = ps2ser_in(UART_IIR);
  242. #endif
  243. if (chr < 0) continue;
  244. if (atomic_read(&ps2buf_cnt) < PS2BUF_SIZE) {
  245. ps2buf[ps2buf_in_idx++] = chr;
  246. ps2buf_in_idx &= (PS2BUF_SIZE - 1);
  247. atomic_inc(&ps2buf_cnt);
  248. } else {
  249. printf ("ps2ser.c: buffer overflow\n");
  250. }
  251. #ifdef CONFIG_MPC5xxx
  252. } while (status & PSC_SR_RXRDY);
  253. #elif defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
  254. } while (status & LSR_DR);
  255. #else
  256. } while (status & UART_IIR_RDI);
  257. #endif
  258. if (atomic_read(&ps2buf_cnt)) {
  259. ps2mult_callback(atomic_read(&ps2buf_cnt));
  260. }
  261. }
  262. #endif /* CONFIG_PS2SERIAL */