ps2ser.c 7.8 KB

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