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