ns16550.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
  21. #define serial_out(x, y) out_be32(y, x)
  22. #define serial_in(y) in_be32(y)
  23. #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
  24. #define serial_out(x, y) out_le32(y, x)
  25. #define serial_in(y) in_le32(y)
  26. #else
  27. #define serial_out(x, y) writeb(x, y)
  28. #define serial_in(y) readb(y)
  29. #endif
  30. #ifndef CONFIG_SYS_NS16550_IER
  31. #define CONFIG_SYS_NS16550_IER 0x00
  32. #endif /* CONFIG_SYS_NS16550_IER */
  33. void NS16550_init(NS16550_t com_port, int baud_divisor)
  34. {
  35. #if (defined(CONFIG_SPL_BUILD) && defined(CONFIG_OMAP34XX))
  36. /*
  37. * On some OMAP3 devices when UART3 is configured for boot mode before
  38. * SPL starts only THRE bit is set. We have to empty the transmitter
  39. * before initialization starts.
  40. */
  41. if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
  42. == UART_LSR_THRE) {
  43. serial_out(UART_LCR_DLAB, &com_port->lcr);
  44. serial_out(baud_divisor & 0xff, &com_port->dll);
  45. serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
  46. serial_out(UART_LCRVAL, &com_port->lcr);
  47. serial_out(0, &com_port->mdr1);
  48. }
  49. #endif
  50. while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
  51. ;
  52. serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
  53. #if (defined(CONFIG_OMAP) && !defined(CONFIG_OMAP3_ZOOM2)) || \
  54. defined(CONFIG_AM33XX) || defined(CONFIG_TI814X)
  55. serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
  56. #endif
  57. serial_out(UART_LCR_BKSE | UART_LCRVAL, &com_port->lcr);
  58. serial_out(0, &com_port->dll);
  59. serial_out(0, &com_port->dlm);
  60. serial_out(UART_LCRVAL, &com_port->lcr);
  61. serial_out(UART_MCRVAL, &com_port->mcr);
  62. serial_out(UART_FCRVAL, &com_port->fcr);
  63. serial_out(UART_LCR_BKSE | UART_LCRVAL, &com_port->lcr);
  64. serial_out(baud_divisor & 0xff, &com_port->dll);
  65. serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
  66. serial_out(UART_LCRVAL, &com_port->lcr);
  67. #if (defined(CONFIG_OMAP) && !defined(CONFIG_OMAP3_ZOOM2)) || \
  68. defined(CONFIG_AM33XX) || defined(CONFIG_SOC_DA8XX) || \
  69. defined(CONFIG_TI814X)
  70. #if defined(CONFIG_APTIX)
  71. /* /13 mode so Aptix 6MHz can hit 115200 */
  72. serial_out(3, &com_port->mdr1);
  73. #else
  74. /* /16 is proper to hit 115200 with 48MHz */
  75. serial_out(0, &com_port->mdr1);
  76. #endif
  77. #endif /* CONFIG_OMAP */
  78. }
  79. #ifndef CONFIG_NS16550_MIN_FUNCTIONS
  80. void NS16550_reinit(NS16550_t com_port, int baud_divisor)
  81. {
  82. serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
  83. serial_out(UART_LCR_BKSE | UART_LCRVAL, &com_port->lcr);
  84. serial_out(0, &com_port->dll);
  85. serial_out(0, &com_port->dlm);
  86. serial_out(UART_LCRVAL, &com_port->lcr);
  87. serial_out(UART_MCRVAL, &com_port->mcr);
  88. serial_out(UART_FCRVAL, &com_port->fcr);
  89. serial_out(UART_LCR_BKSE, &com_port->lcr);
  90. serial_out(baud_divisor & 0xff, &com_port->dll);
  91. serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
  92. serial_out(UART_LCRVAL, &com_port->lcr);
  93. }
  94. #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
  95. void NS16550_putc(NS16550_t com_port, char c)
  96. {
  97. while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
  98. ;
  99. serial_out(c, &com_port->thr);
  100. /*
  101. * Call watchdog_reset() upon newline. This is done here in putc
  102. * since the environment code uses a single puts() to print the complete
  103. * environment upon "printenv". So we can't put this watchdog call
  104. * in puts().
  105. */
  106. if (c == '\n')
  107. WATCHDOG_RESET();
  108. }
  109. #ifndef CONFIG_NS16550_MIN_FUNCTIONS
  110. char NS16550_getc(NS16550_t com_port)
  111. {
  112. while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
  113. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
  114. extern void usbtty_poll(void);
  115. usbtty_poll();
  116. #endif
  117. WATCHDOG_RESET();
  118. }
  119. return serial_in(&com_port->rbr);
  120. }
  121. int NS16550_tstc(NS16550_t com_port)
  122. {
  123. return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
  124. }
  125. #endif /* CONFIG_NS16550_MIN_FUNCTIONS */