gen550_dbg.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * A library of polled 16550 serial routines. These are intended to
  3. * be used to support progress messages, xmon, kgdb, etc. on a
  4. * variety of platforms.
  5. *
  6. * Adapted from lots of code ripped from the arch/ppc/boot/ polled
  7. * 16550 support.
  8. *
  9. * Author: Matt Porter <mporter@mvista.com>
  10. *
  11. * 2002-2003 (c) MontaVista Software, Inc. This file is licensed under
  12. * the terms of the GNU General Public License version 2. This program
  13. * is licensed "as is" without any warranty of any kind, whether express
  14. * or implied.
  15. */
  16. #include <linux/config.h>
  17. #include <linux/types.h>
  18. #include <linux/serial.h>
  19. #include <linux/tty.h> /* For linux/serial_core.h */
  20. #include <linux/serial_core.h>
  21. #include <linux/serialP.h>
  22. #include <linux/serial_reg.h>
  23. #include <asm/machdep.h>
  24. #include <asm/serial.h>
  25. #include <asm/io.h>
  26. #define SERIAL_BAUD 9600
  27. /* SERIAL_PORT_DFNS is defined in <asm/serial.h> */
  28. #ifndef SERIAL_PORT_DFNS
  29. #define SERIAL_PORT_DFNS
  30. #endif
  31. static struct serial_state rs_table[RS_TABLE_SIZE] = {
  32. SERIAL_PORT_DFNS /* defined in <asm/serial.h> */
  33. };
  34. static void (*serial_outb)(unsigned long, unsigned char);
  35. static unsigned long (*serial_inb)(unsigned long);
  36. static int shift;
  37. unsigned long direct_inb(unsigned long addr)
  38. {
  39. return readb((void __iomem *)addr);
  40. }
  41. void direct_outb(unsigned long addr, unsigned char val)
  42. {
  43. writeb(val, (void __iomem *)addr);
  44. }
  45. unsigned long io_inb(unsigned long port)
  46. {
  47. return inb(port);
  48. }
  49. void io_outb(unsigned long port, unsigned char val)
  50. {
  51. outb(val, port);
  52. }
  53. unsigned long serial_init(int chan, void *ignored)
  54. {
  55. unsigned long com_port;
  56. unsigned char lcr, dlm;
  57. /* We need to find out which type io we're expecting. If it's
  58. * 'SERIAL_IO_PORT', we get an offset from the isa_io_base.
  59. * If it's 'SERIAL_IO_MEM', we can the exact location. -- Tom */
  60. switch (rs_table[chan].io_type) {
  61. case SERIAL_IO_PORT:
  62. com_port = rs_table[chan].port;
  63. serial_outb = io_outb;
  64. serial_inb = io_inb;
  65. break;
  66. case SERIAL_IO_MEM:
  67. com_port = (unsigned long)rs_table[chan].iomem_base;
  68. serial_outb = direct_outb;
  69. serial_inb = direct_inb;
  70. break;
  71. default:
  72. /* We can't deal with it. */
  73. return -1;
  74. }
  75. /* How far apart the registers are. */
  76. shift = rs_table[chan].iomem_reg_shift;
  77. /* save the LCR */
  78. lcr = serial_inb(com_port + (UART_LCR << shift));
  79. /* Access baud rate */
  80. serial_outb(com_port + (UART_LCR << shift), UART_LCR_DLAB);
  81. dlm = serial_inb(com_port + (UART_DLM << shift));
  82. /*
  83. * Test if serial port is unconfigured
  84. * We assume that no-one uses less than 110 baud or
  85. * less than 7 bits per character these days.
  86. * -- paulus.
  87. */
  88. if ((dlm <= 4) && (lcr & 2)) {
  89. /* port is configured, put the old LCR back */
  90. serial_outb(com_port + (UART_LCR << shift), lcr);
  91. }
  92. else {
  93. /* Input clock. */
  94. serial_outb(com_port + (UART_DLL << shift),
  95. (rs_table[chan].baud_base / SERIAL_BAUD) & 0xFF);
  96. serial_outb(com_port + (UART_DLM << shift),
  97. (rs_table[chan].baud_base / SERIAL_BAUD) >> 8);
  98. /* 8 data, 1 stop, no parity */
  99. serial_outb(com_port + (UART_LCR << shift), 0x03);
  100. /* RTS/DTR */
  101. serial_outb(com_port + (UART_MCR << shift), 0x03);
  102. /* Clear & enable FIFOs */
  103. serial_outb(com_port + (UART_FCR << shift), 0x07);
  104. }
  105. return (com_port);
  106. }
  107. void
  108. serial_putc(unsigned long com_port, unsigned char c)
  109. {
  110. while ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_THRE) == 0)
  111. ;
  112. serial_outb(com_port, c);
  113. }
  114. unsigned char
  115. serial_getc(unsigned long com_port)
  116. {
  117. while ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) == 0)
  118. ;
  119. return serial_inb(com_port);
  120. }
  121. int
  122. serial_tstc(unsigned long com_port)
  123. {
  124. return ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) != 0);
  125. }
  126. void
  127. serial_close(unsigned long com_port)
  128. {
  129. }
  130. void
  131. gen550_init(int i, struct uart_port *serial_req)
  132. {
  133. rs_table[i].io_type = serial_req->iotype;
  134. rs_table[i].port = serial_req->iobase;
  135. rs_table[i].iomem_base = serial_req->membase;
  136. rs_table[i].iomem_reg_shift = serial_req->regshift;
  137. rs_table[i].baud_base = serial_req->uartclk ? serial_req->uartclk / 16 : BASE_BAUD;
  138. }
  139. #ifdef CONFIG_SERIAL_TEXT_DEBUG
  140. void
  141. gen550_progress(char *s, unsigned short hex)
  142. {
  143. volatile unsigned int progress_debugport;
  144. volatile char c;
  145. progress_debugport = serial_init(0, NULL);
  146. serial_putc(progress_debugport, '\r');
  147. while ((c = *s++) != 0)
  148. serial_putc(progress_debugport, c);
  149. serial_putc(progress_debugport, '\n');
  150. serial_putc(progress_debugport, '\r');
  151. }
  152. #endif /* CONFIG_SERIAL_TEXT_DEBUG */