ns16550.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * COM1 NS16550 support
  3. * originally from linux source (arch/powerpc/boot/ns16550.c)
  4. * modified to use CONFIG_SYS_ISA_MEM and new defines
  5. */
  6. #include <config.h>
  7. #include <ns16550.h>
  8. #include <watchdog.h>
  9. #include <linux/types.h>
  10. #include <asm/io.h>
  11. #define UART_LCRVAL UART_LCR_8N1 /* 8 data, 1 stop, no parity */
  12. #define UART_MCRVAL (UART_MCR_DTR | \
  13. UART_MCR_RTS) /* RTS/DTR */
  14. #define UART_FCRVAL (UART_FCR_FIFO_EN | \
  15. UART_FCR_RXSR | \
  16. UART_FCR_TXSR) /* Clear & enable FIFOs */
  17. #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
  18. #define serial_out(x,y) outb(x,(ulong)y)
  19. #define serial_in(y) inb((ulong)y)
  20. #else
  21. #define serial_out(x,y) writeb(x,y)
  22. #define serial_in(y) readb(y)
  23. #endif
  24. void NS16550_init (NS16550_t com_port, int baud_divisor)
  25. {
  26. serial_out(0x00, &com_port->ier);
  27. #if defined(CONFIG_OMAP) && !defined(CONFIG_OMAP3_ZOOM2)
  28. serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
  29. #endif
  30. serial_out(UART_LCR_BKSE | UART_LCRVAL, (ulong)&com_port->lcr);
  31. serial_out(0, &com_port->dll);
  32. serial_out(0, &com_port->dlm);
  33. serial_out(UART_LCRVAL, &com_port->lcr);
  34. serial_out(UART_MCRVAL, &com_port->mcr);
  35. serial_out(UART_FCRVAL, &com_port->fcr);
  36. serial_out(UART_LCR_BKSE | UART_LCRVAL, &com_port->lcr);
  37. serial_out(baud_divisor & 0xff, &com_port->dll);
  38. serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
  39. serial_out(UART_LCRVAL, &com_port->lcr);
  40. #if defined(CONFIG_OMAP) && !defined(CONFIG_OMAP3_ZOOM2)
  41. #if defined(CONFIG_APTIX)
  42. serial_out(3, &com_port->mdr1); /* /13 mode so Aptix 6MHz can hit 115200 */
  43. #else
  44. serial_out(0, &com_port->mdr1); /* /16 is proper to hit 115200 with 48MHz */
  45. #endif
  46. #endif /* CONFIG_OMAP */
  47. }
  48. #ifndef CONFIG_NS16550_MIN_FUNCTIONS
  49. void NS16550_reinit (NS16550_t com_port, int baud_divisor)
  50. {
  51. serial_out(0x00, &com_port->ier);
  52. serial_out(UART_LCR_BKSE | UART_LCRVAL, &com_port->lcr);
  53. serial_out(0, &com_port->dll);
  54. serial_out(0, &com_port->dlm);
  55. serial_out(UART_LCRVAL, &com_port->lcr);
  56. serial_out(UART_MCRVAL, &com_port->mcr);
  57. serial_out(UART_FCRVAL, &com_port->fcr);
  58. serial_out(UART_LCR_BKSE, &com_port->lcr);
  59. serial_out(baud_divisor & 0xff, &com_port->dll);
  60. serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
  61. serial_out(UART_LCRVAL, &com_port->lcr);
  62. }
  63. #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
  64. void NS16550_putc (NS16550_t com_port, char c)
  65. {
  66. while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0);
  67. serial_out(c, &com_port->thr);
  68. /*
  69. * Call watchdog_reset() upon newline. This is done here in putc
  70. * since the environment code uses a single puts() to print the complete
  71. * environment upon "printenv". So we can't put this watchdog call
  72. * in puts().
  73. */
  74. if (c == '\n')
  75. WATCHDOG_RESET();
  76. }
  77. #ifndef CONFIG_NS16550_MIN_FUNCTIONS
  78. char NS16550_getc (NS16550_t com_port)
  79. {
  80. while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
  81. #ifdef CONFIG_USB_TTY
  82. extern void usbtty_poll(void);
  83. usbtty_poll();
  84. #endif
  85. WATCHDOG_RESET();
  86. }
  87. return serial_in(&com_port->rbr);
  88. }
  89. int NS16550_tstc (NS16550_t com_port)
  90. {
  91. return ((serial_in(&com_port->lsr) & UART_LSR_DR) != 0);
  92. }
  93. #endif /* CONFIG_NS16550_MIN_FUNCTIONS */