]>
git.saurik.com Git - apple/xnu.git/blob - pexpert/i386/pe_serial.c
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 * Polled-mode 16x50 UART driver.
34 #include <pexpert/protos.h>
35 #include <pexpert/pexpert.h>
37 /* standard port addresses */
39 COM1_PORT_ADDR
= 0x3f8,
40 COM2_PORT_ADDR
= 0x2f8
43 /* UART register offsets */
45 UART_RBR
= 0, /* receive buffer Register (R) */
46 UART_THR
= 0, /* transmit holding register (W) */
47 UART_DLL
= 0, /* DLAB = 1, divisor latch (LSB) */
48 UART_IER
= 1, /* interrupt enable register */
49 UART_DLM
= 1, /* DLAB = 1, divisor latch (MSB) */
50 UART_IIR
= 2, /* interrupt ident register (R) */
51 UART_FCR
= 2, /* fifo control register (W) */
52 UART_LCR
= 3, /* line control register */
53 UART_MCR
= 4, /* modem control register */
54 UART_LSR
= 5, /* line status register */
55 UART_MSR
= 6, /* modem status register */
56 UART_SCR
= 7 /* scratch register */
60 UART_LCR_8BITS
= 0x03,
80 static unsigned uart_baud_rate
= 115200;
81 #define UART_PORT_ADDR COM1_PORT_ADDR
83 #define UART_CLOCK 1843200 /* 1.8432 MHz clock */
85 #define WRITE(r, v) outb(UART_PORT_ADDR + UART_##r, v)
86 #define READ(r) inb(UART_PORT_ADDR + UART_##r)
87 #define DELAY(x) { volatile int _d_; for (_d_ = 0; _d_ < (10000*x); _d_++) ; }
89 static int uart_initted
= 0; /* 1 if init'ed */
94 /* Verify that the Scratch Register is accessible */
97 if (READ(SCR
) != 0x5a) return 0;
99 if (READ(SCR
) != 0xa5) return 0;
104 uart_set_baud_rate( unsigned long baud_rate
)
106 const unsigned char lcr
= READ( LCR
);
109 if (baud_rate
== 0) baud_rate
= 9600;
110 div
= UART_CLOCK
/ 16 / baud_rate
;
111 WRITE( LCR
, lcr
| UART_LCR_DLAB
);
112 WRITE( DLM
, (unsigned char)(div
>> 8) );
113 WRITE( DLL
, (unsigned char) div
);
114 WRITE( LCR
, lcr
& ~UART_LCR_DLAB
);
120 if (!uart_initted
) return;
122 /* Wait for THR empty */
123 while ( !(READ(LSR
) & UART_LSR_THRE
) ) DELAY(1);
132 * This function returns:
134 * -2 : receiver error
135 * >0 : character received
140 if (!uart_initted
) return -1;
144 if ( lsr
& (UART_LSR_FE
| UART_LSR_PE
| UART_LSR_OE
) )
146 READ( RBR
); /* discard */
150 if ( lsr
& UART_LSR_DR
)
158 int serial_init( void )
160 unsigned serial_baud_rate
= 0;
162 if ( /*uart_initted ||*/ uart_probe() == 0 ) return 0;
164 /* Disable hardware interrupts */
169 /* Disable FIFO's for 16550 devices */
173 /* Set for 8-bit, no parity, DLAB bit cleared */
175 WRITE( LCR
, UART_LCR_8BITS
);
177 /* Set baud rate - use the supplied boot-arg if available */
179 if (PE_parse_boot_argn("serialbaud", &serial_baud_rate
, sizeof (serial_baud_rate
)))
182 if (!((UART_CLOCK
/ 16) % serial_baud_rate
)) {
183 uart_baud_rate
= serial_baud_rate
;
186 uart_set_baud_rate( uart_baud_rate
);
188 /* Assert DTR# and RTS# lines (OUT2?) */
190 WRITE( MCR
, UART_MCR_DTR
| UART_MCR_RTS
);
192 /* Clear any garbage in the input buffer */
201 void serial_putc( char c
)
204 if (c
== '\n') uart_putc('\r');
207 int serial_getc( void )