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 <machine/machine_routines.h>
35 #include <pexpert/protos.h>
36 #include <pexpert/pexpert.h>
38 struct pe_serial_functions
{
39 void (*uart_init
) (void);
40 void (*uart_set_baud_rate
) (int unit
, uint32_t baud_rate
);
47 static struct pe_serial_functions
*gPESF
;
49 static int uart_initted
= 0; /* 1 if init'ed */
51 #define DEFAULT_UART_BAUD_RATE 115200
53 static unsigned uart_baud_rate
= DEFAULT_UART_BAUD_RATE
;
55 // =============================================================================
56 // Legacy UART support using IO transactions to COM1 or COM2
57 // =============================================================================
59 #define LEGACY_UART_PORT_ADDR COM1_PORT_ADDR
60 #define LEGACY_UART_CLOCK 1843200 /* 1.8432 MHz clock */
62 #define IO_WRITE(r, v) outb(LEGACY_UART_PORT_ADDR + UART_##r, v)
63 #define IO_READ(r) inb(LEGACY_UART_PORT_ADDR + UART_##r)
66 COM1_PORT_ADDR
= 0x3f8,
67 COM2_PORT_ADDR
= 0x2f8
71 UART_RBR
= 0, /* receive buffer Register (R) */
72 UART_THR
= 0, /* transmit holding register (W) */
73 UART_DLL
= 0, /* DLAB = 1, divisor latch (LSB) */
74 UART_IER
= 1, /* interrupt enable register */
75 UART_DLM
= 1, /* DLAB = 1, divisor latch (MSB) */
76 UART_IIR
= 2, /* interrupt ident register (R) */
77 UART_FCR
= 2, /* fifo control register (W) */
78 UART_LCR
= 3, /* line control register */
79 UART_MCR
= 4, /* modem control register */
80 UART_LSR
= 5, /* line status register */
81 UART_MSR
= 6, /* modem status register */
82 UART_SCR
= 7 /* scratch register */
86 UART_LCR_8BITS
= 0x03,
107 legacy_uart_probe( void )
109 /* Verify that the Scratch Register is accessible */
111 IO_WRITE( SCR
, 0x5a );
112 if (IO_READ(SCR
) != 0x5a) return 0;
113 IO_WRITE( SCR
, 0xa5 );
114 if (IO_READ(SCR
) != 0xa5) return 0;
119 legacy_uart_set_baud_rate( __unused
int unit
, uint32_t baud_rate
)
121 const unsigned char lcr
= IO_READ( LCR
);
124 if (baud_rate
== 0) baud_rate
= 9600;
125 div
= LEGACY_UART_CLOCK
/ 16 / baud_rate
;
126 IO_WRITE( LCR
, lcr
| UART_LCR_DLAB
);
127 IO_WRITE( DLM
, (unsigned char)(div
>> 8) );
128 IO_WRITE( DLL
, (unsigned char) div
);
129 IO_WRITE( LCR
, lcr
& ~UART_LCR_DLAB
);
133 legacy_uart_tr0( void )
135 return (IO_READ(LSR
) & UART_LSR_THRE
);
139 legacy_uart_td0( int c
)
145 legacy_uart_init( void )
147 /* Disable hardware interrupts */
152 /* Disable FIFO's for 16550 devices */
156 /* Set for 8-bit, no parity, DLAB bit cleared */
158 IO_WRITE( LCR
, UART_LCR_8BITS
);
162 gPESF
->uart_set_baud_rate ( 0, uart_baud_rate
);
164 /* Assert DTR# and RTS# lines (OUT2?) */
166 IO_WRITE( MCR
, UART_MCR_DTR
| UART_MCR_RTS
);
168 /* Clear any garbage in the input buffer */
176 legacy_uart_rr0( void )
180 lsr
= IO_READ( LSR
);
182 if ( lsr
& (UART_LSR_FE
| UART_LSR_PE
| UART_LSR_OE
) )
184 IO_READ( RBR
); /* discard */
188 return (lsr
& UART_LSR_DR
);
192 legacy_uart_rd0( void )
194 return IO_READ( RBR
);
197 static struct pe_serial_functions legacy_uart_serial_functions
= {
198 .uart_init
= legacy_uart_init
,
199 .uart_set_baud_rate
= legacy_uart_set_baud_rate
,
200 .tr0
= legacy_uart_tr0
,
201 .td0
= legacy_uart_td0
,
202 .rr0
= legacy_uart_rr0
,
203 .rd0
= legacy_uart_rd0
206 // =============================================================================
207 // MMIO UART (using PCH LPSS UART2)
208 // =============================================================================
210 #define MMIO_UART2_BASE_LEGACY 0xFE034000
211 #define MMIO_UART2_BASE 0xFE036000
213 #define MMIO_WRITE(r, v) ml_phys_write_word(mmio_uart_base + MMIO_UART_##r, v)
214 #define MMIO_READ(r) ml_phys_read_word(mmio_uart_base + MMIO_UART_##r)
217 MMIO_UART_RBR
= 0x0, /* receive buffer Register (R) */
218 MMIO_UART_THR
= 0x0, /* transmit holding register (W) */
219 MMIO_UART_DLL
= 0x0, /* DLAB = 1, divisor latch (LSB) */
220 MMIO_UART_IER
= 0x4, /* interrupt enable register */
221 MMIO_UART_DLM
= 0x4, /* DLAB = 1, divisor latch (MSB) */
222 MMIO_UART_FCR
= 0x8, /* fifo control register (W) */
223 MMIO_UART_LCR
= 0xc, /* line control register */
224 MMIO_UART_MCR
= 0x10, /* modem control register */
225 MMIO_UART_LSR
= 0x14, /* line status register */
226 MMIO_UART_SCR
= 0x1c /* scratch register */
229 static vm_offset_t mmio_uart_base
= 0;
232 mmio_uart_present( void )
234 MMIO_WRITE( SCR
, 0x5a );
235 if (MMIO_READ(SCR
) != 0x5a) return 0;
236 MMIO_WRITE( SCR
, 0xa5 );
237 if (MMIO_READ(SCR
) != 0xa5) return 0;
243 mmio_uart_probe( void )
245 unsigned new_mmio_uart_base
= 0;
247 // if specified, mmio_uart overrides all probing
248 if (PE_parse_boot_argn("mmio_uart", &new_mmio_uart_base
, sizeof (new_mmio_uart_base
)))
250 // mmio_uart=0 will disable mmio_uart support
251 if (new_mmio_uart_base
== 0) {
255 mmio_uart_base
= new_mmio_uart_base
;
259 // probe the two possible MMIO_UART2 addresses
260 mmio_uart_base
= MMIO_UART2_BASE
;
261 if (mmio_uart_present()) {
265 mmio_uart_base
= MMIO_UART2_BASE_LEGACY
;
266 if (mmio_uart_present()) {
270 // no mmio uart found
275 mmio_uart_set_baud_rate( __unused
int unit
, __unused
uint32_t baud_rate
)
277 const unsigned char lcr
= MMIO_READ( LCR
);
280 if (baud_rate
== 0) baud_rate
= 9600;
281 div
= LEGACY_UART_CLOCK
/ 16 / baud_rate
;
283 MMIO_WRITE( LCR
, lcr
| UART_LCR_DLAB
);
284 MMIO_WRITE( DLM
, (unsigned char)(div
>> 8) );
285 MMIO_WRITE( DLL
, (unsigned char) div
);
286 MMIO_WRITE( LCR
, lcr
& ~UART_LCR_DLAB
);
290 mmio_uart_tr0( void )
292 return (MMIO_READ(LSR
) & UART_LSR_THRE
);
296 mmio_uart_td0( int c
)
298 MMIO_WRITE( THR
, c
);
302 mmio_uart_init( void )
304 /* Disable hardware interrupts */
306 MMIO_WRITE( MCR
, 0 );
307 MMIO_WRITE( IER
, 0 );
309 /* Disable FIFO's for 16550 devices */
311 MMIO_WRITE( FCR
, 0 );
313 /* Set for 8-bit, no parity, DLAB bit cleared */
315 MMIO_WRITE( LCR
, UART_LCR_8BITS
);
317 /* Leave baud rate as set by firmware unless serialbaud boot-arg overrides */
319 if (uart_baud_rate
!= DEFAULT_UART_BAUD_RATE
)
321 gPESF
->uart_set_baud_rate ( 0, uart_baud_rate
);
324 /* Assert DTR# and RTS# lines (OUT2?) */
326 MMIO_WRITE( MCR
, UART_MCR_DTR
| UART_MCR_RTS
);
328 /* Clear any garbage in the input buffer */
336 mmio_uart_rr0( void )
340 lsr
= MMIO_READ( LSR
);
342 if ( lsr
& (UART_LSR_FE
| UART_LSR_PE
| UART_LSR_OE
) )
344 MMIO_READ( RBR
); /* discard */
348 return (lsr
& UART_LSR_DR
);
352 mmio_uart_rd0( void )
354 return MMIO_READ( RBR
);
357 static struct pe_serial_functions mmio_uart_serial_functions
= {
358 .uart_init
= mmio_uart_init
,
359 .uart_set_baud_rate
= mmio_uart_set_baud_rate
,
360 .tr0
= mmio_uart_tr0
,
361 .td0
= mmio_uart_td0
,
362 .rr0
= mmio_uart_rr0
,
366 // =============================================================================
367 // Generic serial support below
368 // =============================================================================
373 unsigned new_uart_baud_rate
= 0;
375 if (PE_parse_boot_argn("serialbaud", &new_uart_baud_rate
, sizeof (new_uart_baud_rate
)))
378 if (!((LEGACY_UART_CLOCK
/ 16) % new_uart_baud_rate
)) {
379 uart_baud_rate
= new_uart_baud_rate
;
383 if ( mmio_uart_probe() )
385 gPESF
= &mmio_uart_serial_functions
;
389 else if ( legacy_uart_probe() )
391 gPESF
= &legacy_uart_serial_functions
;
406 while (!gPESF
->tr0()); /* Wait until THR is empty. */
423 serial_putc( char c
)