ps2ser.c 7.8 KB

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