ns16550.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * COM1 NS16550 support
  3. */
  4. #include <linux/config.h>
  5. #include <linux/types.h>
  6. #include <linux/serial.h>
  7. #include <linux/serial_reg.h>
  8. #include <asm/serial.h>
  9. #include "nonstdio.h"
  10. #include "serial.h"
  11. #define SERIAL_BAUD 9600
  12. extern unsigned long ISA_io;
  13. static struct serial_state rs_table[RS_TABLE_SIZE] = {
  14. SERIAL_PORT_DFNS /* Defined in <asm/serial.h> */
  15. };
  16. static int shift;
  17. unsigned long serial_init(int chan, void *ignored)
  18. {
  19. unsigned long com_port, base_baud;
  20. unsigned char lcr, dlm;
  21. /* We need to find out which type io we're expecting. If it's
  22. * 'SERIAL_IO_PORT', we get an offset from the isa_io_base.
  23. * If it's 'SERIAL_IO_MEM', we can the exact location. -- Tom */
  24. switch (rs_table[chan].io_type) {
  25. case SERIAL_IO_PORT:
  26. com_port = rs_table[chan].port;
  27. break;
  28. case SERIAL_IO_MEM:
  29. com_port = (unsigned long)rs_table[chan].iomem_base;
  30. break;
  31. default:
  32. /* We can't deal with it. */
  33. return -1;
  34. }
  35. /* How far apart the registers are. */
  36. shift = rs_table[chan].iomem_reg_shift;
  37. /* Base baud.. */
  38. base_baud = rs_table[chan].baud_base;
  39. /* save the LCR */
  40. lcr = inb(com_port + (UART_LCR << shift));
  41. /* Access baud rate */
  42. outb(com_port + (UART_LCR << shift), 0x80);
  43. dlm = inb(com_port + (UART_DLM << shift));
  44. /*
  45. * Test if serial port is unconfigured.
  46. * We assume that no-one uses less than 110 baud or
  47. * less than 7 bits per character these days.
  48. * -- paulus.
  49. */
  50. if ((dlm <= 4) && (lcr & 2))
  51. /* port is configured, put the old LCR back */
  52. outb(com_port + (UART_LCR << shift), lcr);
  53. else {
  54. /* Input clock. */
  55. outb(com_port + (UART_DLL << shift),
  56. (base_baud / SERIAL_BAUD) & 0xFF);
  57. outb(com_port + (UART_DLM << shift),
  58. (base_baud / SERIAL_BAUD) >> 8);
  59. /* 8 data, 1 stop, no parity */
  60. outb(com_port + (UART_LCR << shift), 0x03);
  61. /* RTS/DTR */
  62. outb(com_port + (UART_MCR << shift), 0x03);
  63. }
  64. /* Clear & enable FIFOs */
  65. outb(com_port + (UART_FCR << shift), 0x07);
  66. return (com_port);
  67. }
  68. void
  69. serial_putc(unsigned long com_port, unsigned char c)
  70. {
  71. while ((inb(com_port + (UART_LSR << shift)) & UART_LSR_THRE) == 0)
  72. ;
  73. outb(com_port, c);
  74. }
  75. unsigned char
  76. serial_getc(unsigned long com_port)
  77. {
  78. while ((inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) == 0)
  79. ;
  80. return inb(com_port);
  81. }
  82. int
  83. serial_tstc(unsigned long com_port)
  84. {
  85. return ((inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) != 0);
  86. }