gen550_dbg.c 4.4 KB

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