]>
Commit | Line | Data |
---|---|---|
55e303ae A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
55e303ae | 5 | * |
8f6c56a5 A |
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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
55e303ae A |
27 | */ |
28 | ||
29 | /* | |
30 | * file: pe_serial.c | |
31 | * Polled-mode 16x50 UART driver. | |
32 | */ | |
33 | ||
34 | #include <pexpert/protos.h> | |
35 | #include <pexpert/pexpert.h> | |
36 | ||
91447636 A |
37 | void serial_putc(char); |
38 | int serial_getc(void); | |
39 | int serial_init(void); | |
40 | ||
55e303ae A |
41 | /* standard port addresses */ |
42 | enum { | |
43 | COM1_PORT_ADDR = 0x3f8, | |
44 | COM2_PORT_ADDR = 0x2f8 | |
45 | }; | |
46 | ||
47 | /* UART register offsets */ | |
48 | enum { | |
49 | UART_RBR = 0, /* receive buffer Register (R) */ | |
50 | UART_THR = 0, /* transmit holding register (W) */ | |
51 | UART_DLL = 0, /* DLAB = 1, divisor latch (LSB) */ | |
52 | UART_IER = 1, /* interrupt enable register */ | |
53 | UART_DLM = 1, /* DLAB = 1, divisor latch (MSB) */ | |
54 | UART_IIR = 2, /* interrupt ident register (R) */ | |
55 | UART_FCR = 2, /* fifo control register (W) */ | |
56 | UART_LCR = 3, /* line control register */ | |
57 | UART_MCR = 4, /* modem control register */ | |
58 | UART_LSR = 5, /* line status register */ | |
59 | UART_MSR = 6 /* modem status register */ | |
60 | }; | |
61 | ||
62 | enum { | |
63 | UART_LCR_8BITS = 0x03, | |
64 | UART_LCR_DLAB = 0x80 | |
65 | }; | |
66 | ||
67 | enum { | |
68 | UART_MCR_DTR = 0x01, | |
69 | UART_MCR_RTS = 0x02, | |
70 | UART_MCR_OUT1 = 0x04, | |
71 | UART_MCR_OUT2 = 0x08, | |
72 | UART_MCR_LOOP = 0x10 | |
73 | }; | |
74 | ||
75 | enum { | |
89b3af67 A |
76 | UART_LSR_DR = 0x01, |
77 | UART_LSR_OE = 0x02, | |
78 | UART_LSR_PE = 0x04, | |
79 | UART_LSR_FE = 0x08, | |
55e303ae A |
80 | UART_LSR_THRE = 0x20 |
81 | }; | |
82 | ||
89b3af67 | 83 | static unsigned uart_baud_rate = 115200; |
55e303ae A |
84 | #define UART_PORT_ADDR COM1_PORT_ADDR |
85 | ||
89b3af67 A |
86 | #define UART_CLOCK 1843200 /* 1.8432 MHz clock */ |
87 | ||
55e303ae A |
88 | #define WRITE(r, v) outb(UART_PORT_ADDR + UART_##r, v) |
89 | #define READ(r) inb(UART_PORT_ADDR + UART_##r) | |
90 | #define DELAY(x) { volatile int _d_; for (_d_ = 0; _d_ < (10000*x); _d_++) ; } | |
91 | ||
92 | static int uart_initted = 0; /* 1 if init'ed */ | |
93 | ||
94 | static int | |
95 | uart_probe( void ) | |
96 | { | |
97 | /* Verify that the Divisor Register is accessible */ | |
98 | ||
99 | WRITE( LCR, UART_LCR_DLAB ); | |
100 | WRITE( DLL, 0x5a ); | |
101 | if (READ(DLL) != 0x5a) return 0; | |
102 | WRITE( DLL, 0xa5 ); | |
103 | if (READ(DLL) != 0xa5) return 0; | |
104 | WRITE( LCR, 0x00 ); | |
105 | return 1; | |
106 | } | |
107 | ||
108 | static void | |
109 | uart_set_baud_rate( unsigned long baud_rate ) | |
110 | { | |
55e303ae A |
111 | const unsigned char lcr = READ( LCR ); |
112 | unsigned long div; | |
113 | ||
114 | if (baud_rate == 0) baud_rate = 9600; | |
115 | div = UART_CLOCK / 16 / baud_rate; | |
116 | WRITE( LCR, lcr | UART_LCR_DLAB ); | |
117 | WRITE( DLM, (unsigned char)(div >> 8) ); | |
118 | WRITE( DLL, (unsigned char) div ); | |
119 | WRITE( LCR, lcr & ~UART_LCR_DLAB); | |
120 | } | |
121 | ||
122 | static void | |
123 | uart_putc( char c ) | |
124 | { | |
125 | if (!uart_initted) return; | |
126 | ||
127 | /* Wait for THR empty */ | |
128 | while ( !(READ(LSR) & UART_LSR_THRE) ) DELAY(1); | |
129 | ||
130 | WRITE( THR, c ); | |
131 | } | |
132 | ||
89b3af67 A |
133 | static int |
134 | uart_getc( void ) | |
135 | { | |
136 | /* | |
137 | * This function returns: | |
138 | * -1 : no data | |
139 | * -2 : receiver error | |
140 | * >0 : character received | |
141 | */ | |
142 | ||
143 | unsigned char lsr; | |
144 | ||
145 | if (!uart_initted) return -1; | |
146 | ||
147 | lsr = READ( LSR ); | |
148 | ||
149 | if ( lsr & (UART_LSR_FE | UART_LSR_PE | UART_LSR_OE) ) | |
150 | { | |
151 | READ( RBR ); /* discard */ | |
152 | return -2; | |
153 | } | |
154 | ||
155 | if ( lsr & UART_LSR_DR ) | |
156 | { | |
157 | return READ( RBR ); | |
158 | } | |
159 | ||
160 | return -1; | |
161 | } | |
162 | ||
55e303ae A |
163 | int serial_init( void ) |
164 | { | |
89b3af67 A |
165 | unsigned serial_baud_rate = 0; |
166 | ||
91447636 | 167 | if ( /*uart_initted ||*/ uart_probe() == 0 ) return 0; |
55e303ae A |
168 | |
169 | /* Disable hardware interrupts */ | |
170 | ||
171 | WRITE( MCR, 0 ); | |
172 | WRITE( IER, 0 ); | |
173 | ||
174 | /* Disable FIFO's for 16550 devices */ | |
175 | ||
176 | WRITE( FCR, 0 ); | |
177 | ||
178 | /* Set for 8-bit, no parity, DLAB bit cleared */ | |
179 | ||
180 | WRITE( LCR, UART_LCR_8BITS ); | |
181 | ||
89b3af67 | 182 | /* Set baud rate - use the supplied boot-arg if available */ |
55e303ae | 183 | |
89b3af67 A |
184 | if (PE_parse_boot_arg("serialbaud", &serial_baud_rate)) |
185 | { | |
186 | /* Valid divisor? */ | |
187 | if (!((UART_CLOCK / 16) % serial_baud_rate)) { | |
188 | uart_baud_rate = serial_baud_rate; | |
189 | } | |
190 | } | |
191 | uart_set_baud_rate( uart_baud_rate ); | |
55e303ae A |
192 | |
193 | /* Assert DTR# and RTS# lines (OUT2?) */ | |
194 | ||
195 | WRITE( MCR, UART_MCR_DTR | UART_MCR_RTS ); | |
196 | ||
197 | /* Clear any garbage in the input buffer */ | |
198 | ||
199 | READ( RBR ); | |
200 | ||
201 | uart_initted = 1; | |
202 | ||
203 | return 1; | |
204 | } | |
205 | ||
206 | void serial_putc( char c ) | |
207 | { | |
208 | uart_putc(c); | |
209 | if (c == '\n') uart_putc('\r'); | |
210 | } | |
211 | ||
212 | int serial_getc( void ) | |
213 | { | |
89b3af67 | 214 | return uart_getc(); |
55e303ae | 215 | } |