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 static unsigned int legacy_uart_enabled
= 0; /* 1 Legacy IO based UART is supported on platform */
53 static boolean_t lpss_uart_supported
= 0; /* 1 if LPSS UART is supported on platform */
54 static unsigned int lpss_uart_enabled
= 0; /* 1 if it is LPSS UART is in D0 state */
55 static void lpss_uart_re_init (void);
57 #define DEFAULT_UART_BAUD_RATE 115200
59 static unsigned uart_baud_rate
= DEFAULT_UART_BAUD_RATE
;
61 // =============================================================================
62 // Legacy UART support using IO transactions to COM1 or COM2
63 // =============================================================================
65 #define LEGACY_UART_PORT_ADDR COM1_PORT_ADDR
66 #define LEGACY_UART_CLOCK 1843200 /* 1.8432 MHz clock */
68 #define IO_WRITE(r, v) outb(LEGACY_UART_PORT_ADDR + UART_##r, v)
69 #define IO_READ(r) inb(LEGACY_UART_PORT_ADDR + UART_##r)
72 COM1_PORT_ADDR
= 0x3f8,
73 COM2_PORT_ADDR
= 0x2f8
77 UART_RBR
= 0, /* receive buffer Register (R) */
78 UART_THR
= 0, /* transmit holding register (W) */
79 UART_DLL
= 0, /* DLAB = 1, divisor latch (LSB) */
80 UART_IER
= 1, /* interrupt enable register */
81 UART_DLM
= 1, /* DLAB = 1, divisor latch (MSB) */
82 UART_IIR
= 2, /* interrupt ident register (R) */
83 UART_FCR
= 2, /* fifo control register (W) */
84 UART_LCR
= 3, /* line control register */
85 UART_MCR
= 4, /* modem control register */
86 UART_LSR
= 5, /* line status register */
87 UART_MSR
= 6, /* modem status register */
88 UART_SCR
= 7 /* scratch register */
92 UART_LCR_8BITS
= 0x03,
100 UART_MCR_OUT2
= 0x08,
113 UART_CLK_125M_1
= 0x60002,
114 UART_CLK_125M_2
= 0x80060003,
118 legacy_uart_probe( void )
120 /* Verify that the Scratch Register is accessible */
122 IO_WRITE( SCR
, 0x5a );
123 if (IO_READ(SCR
) != 0x5a) return 0;
124 IO_WRITE( SCR
, 0xa5 );
125 if (IO_READ(SCR
) != 0xa5) return 0;
130 legacy_uart_set_baud_rate( __unused
int unit
, uint32_t baud_rate
)
132 const unsigned char lcr
= IO_READ( LCR
);
135 if (baud_rate
== 0) baud_rate
= 9600;
136 div
= LEGACY_UART_CLOCK
/ 16 / baud_rate
;
137 IO_WRITE( LCR
, lcr
| UART_LCR_DLAB
);
138 IO_WRITE( DLM
, (unsigned char)(div
>> 8) );
139 IO_WRITE( DLL
, (unsigned char) div
);
140 IO_WRITE( LCR
, lcr
& ~UART_LCR_DLAB
);
144 legacy_uart_tr0( void )
146 return (IO_READ(LSR
) & UART_LSR_THRE
);
150 legacy_uart_td0( int c
)
156 legacy_uart_init( void )
158 /* Disable hardware interrupts */
163 /* Disable FIFO's for 16550 devices */
167 /* Set for 8-bit, no parity, DLAB bit cleared */
169 IO_WRITE( LCR
, UART_LCR_8BITS
);
173 gPESF
->uart_set_baud_rate ( 0, uart_baud_rate
);
175 /* Assert DTR# and RTS# lines (OUT2?) */
177 IO_WRITE( MCR
, UART_MCR_DTR
| UART_MCR_RTS
);
179 /* Clear any garbage in the input buffer */
187 legacy_uart_rr0( void )
191 lsr
= IO_READ( LSR
);
193 if ( lsr
& (UART_LSR_FE
| UART_LSR_PE
| UART_LSR_OE
) )
195 IO_READ( RBR
); /* discard */
199 return (lsr
& UART_LSR_DR
);
203 legacy_uart_rd0( void )
205 return IO_READ( RBR
);
208 static struct pe_serial_functions legacy_uart_serial_functions
= {
209 .uart_init
= legacy_uart_init
,
210 .uart_set_baud_rate
= legacy_uart_set_baud_rate
,
211 .tr0
= legacy_uart_tr0
,
212 .td0
= legacy_uart_td0
,
213 .rr0
= legacy_uart_rr0
,
214 .rd0
= legacy_uart_rd0
217 // =============================================================================
218 // MMIO UART (using PCH LPSS UART2)
219 // =============================================================================
221 #define MMIO_UART2_BASE_LEGACY 0xFE034000 /* Legacy MMIO Config space */
222 #define MMIO_UART2_BASE 0xFE036000 /* MMIO Config space */
223 #define PCI_UART2 0xFE037000 /* PCI Config Space */
225 #define MMIO_WRITE(r, v) ml_phys_write_word(mmio_uart_base + MMIO_UART_##r, v)
226 #define MMIO_READ(r) ml_phys_read_word(mmio_uart_base + MMIO_UART_##r)
229 MMIO_UART_RBR
= 0x0, /* receive buffer Register (R) */
230 MMIO_UART_THR
= 0x0, /* transmit holding register (W) */
231 MMIO_UART_DLL
= 0x0, /* DLAB = 1, divisor latch (LSB) */
232 MMIO_UART_IER
= 0x4, /* interrupt enable register */
233 MMIO_UART_DLM
= 0x4, /* DLAB = 1, divisor latch (MSB) */
234 MMIO_UART_FCR
= 0x8, /* fifo control register (W) */
235 MMIO_UART_LCR
= 0xc, /* line control register */
236 MMIO_UART_MCR
= 0x10, /* modem control register */
237 MMIO_UART_LSR
= 0x14, /* line status register */
238 MMIO_UART_SCR
= 0x1c, /* scratch register */
239 MMIO_UART_CLK
= 0x200, /* clocks register */
240 MMIO_UART_RST
= 0x204 /* Reset register */
243 static vm_offset_t mmio_uart_base
= 0;
246 mmio_uart_present( void )
248 MMIO_WRITE( SCR
, 0x5a );
249 if (MMIO_READ(SCR
) != 0x5a) return 0;
250 MMIO_WRITE( SCR
, 0xa5 );
251 if (MMIO_READ(SCR
) != 0xa5) return 0;
256 mmio_uart_probe( void )
258 unsigned new_mmio_uart_base
= 0;
260 // if specified, mmio_uart overrides all probing
261 if (PE_parse_boot_argn("mmio_uart", &new_mmio_uart_base
, sizeof (new_mmio_uart_base
)))
263 // mmio_uart=0 will disable mmio_uart support
264 if (new_mmio_uart_base
== 0) {
268 mmio_uart_base
= new_mmio_uart_base
;
272 // probe the two possible MMIO_UART2 addresses
273 mmio_uart_base
= MMIO_UART2_BASE
;
274 if (mmio_uart_present()) {
278 mmio_uart_base
= MMIO_UART2_BASE_LEGACY
;
279 if (mmio_uart_present()) {
283 // no mmio uart found
288 mmio_uart_set_baud_rate( __unused
int unit
, __unused
uint32_t baud_rate
)
290 const unsigned char lcr
= MMIO_READ( LCR
);
293 if (baud_rate
== 0) baud_rate
= 9600;
294 div
= LEGACY_UART_CLOCK
/ 16 / baud_rate
;
296 MMIO_WRITE( LCR
, lcr
| UART_LCR_DLAB
);
297 MMIO_WRITE( DLM
, (unsigned char)(div
>> 8) );
298 MMIO_WRITE( DLL
, (unsigned char) div
);
299 MMIO_WRITE( LCR
, lcr
& ~UART_LCR_DLAB
);
303 mmio_uart_tr0( void )
305 return (MMIO_READ(LSR
) & UART_LSR_THRE
);
309 mmio_uart_td0( int c
)
311 MMIO_WRITE( THR
, c
);
315 mmio_uart_init( void )
317 /* Disable hardware interrupts */
319 MMIO_WRITE( MCR
, 0 );
320 MMIO_WRITE( IER
, 0 );
322 /* Disable FIFO's for 16550 devices */
324 MMIO_WRITE( FCR
, 0 );
326 /* Set for 8-bit, no parity, DLAB bit cleared */
328 MMIO_WRITE( LCR
, UART_LCR_8BITS
);
330 /* Leave baud rate as set by firmware unless serialbaud boot-arg overrides */
332 if (uart_baud_rate
!= DEFAULT_UART_BAUD_RATE
)
334 gPESF
->uart_set_baud_rate ( 0, uart_baud_rate
);
337 /* Assert DTR# and RTS# lines (OUT2?) */
339 MMIO_WRITE( MCR
, UART_MCR_DTR
| UART_MCR_RTS
);
341 /* Clear any garbage in the input buffer */
349 mmio_uart_rr0( void )
353 lsr
= MMIO_READ( LSR
);
355 if ( lsr
& (UART_LSR_FE
| UART_LSR_PE
| UART_LSR_OE
) )
357 MMIO_READ( RBR
); /* discard */
361 return (lsr
& UART_LSR_DR
);
364 void lpss_uart_enable( boolean_t on_off
)
366 unsigned int pmcs_reg
;
368 if (!lpss_uart_supported
) {
372 pmcs_reg
= ml_phys_read_byte (PCI_UART2
+ 0x84);
373 if (on_off
== FALSE
) {
375 lpss_uart_enabled
= 0;
380 ml_phys_write_byte (PCI_UART2
+ 0x84, pmcs_reg
);
381 pmcs_reg
= ml_phys_read_byte (PCI_UART2
+ 0x84);
383 if (on_off
== TRUE
) {
385 lpss_uart_enabled
= 1;
389 static void lpss_uart_re_init( void )
391 uint32_t register_read
;
393 MMIO_WRITE (RST
, 0x7); /* LPSS UART2 controller out ot reset */
394 register_read
= MMIO_READ (RST
);
396 MMIO_WRITE (LCR
, UART_LCR_DLAB
); /* Set DLAB bit to enable reading/writing of DLL, DLH */
397 register_read
= MMIO_READ (LCR
);
399 MMIO_WRITE (DLL
, 1); /* Divisor Latch Low Register */
400 register_read
= MMIO_READ (DLL
);
402 MMIO_WRITE (DLM
, 0); /* Divisor Latch High Register */
403 register_read
= MMIO_READ (DLM
);
405 MMIO_WRITE (FCR
, 1); /* Enable FIFO */
406 register_read
= MMIO_READ (FCR
);
408 MMIO_WRITE (LCR
, UART_LCR_8BITS
); /* Set 8 bits, clear DLAB */
409 register_read
= MMIO_READ (LCR
);
411 MMIO_WRITE (MCR
, UART_MCR_RTS
); /* Request to send */
412 register_read
= MMIO_READ (MCR
);
414 MMIO_WRITE (CLK
, UART_CLK_125M_1
); /* 1.25M Clock speed */
415 register_read
= MMIO_READ (CLK
);
417 MMIO_WRITE (CLK
, UART_CLK_125M_2
); /* 1.25M Clock speed */
418 register_read
= MMIO_READ (CLK
);
422 mmio_uart_rd0( void )
424 return MMIO_READ( RBR
);
427 static struct pe_serial_functions mmio_uart_serial_functions
= {
428 .uart_init
= mmio_uart_init
,
429 .uart_set_baud_rate
= mmio_uart_set_baud_rate
,
430 .tr0
= mmio_uart_tr0
,
431 .td0
= mmio_uart_td0
,
432 .rr0
= mmio_uart_rr0
,
436 // =============================================================================
437 // Generic serial support below
438 // =============================================================================
443 unsigned new_uart_baud_rate
= 0;
445 if (PE_parse_boot_argn("serialbaud", &new_uart_baud_rate
, sizeof (new_uart_baud_rate
)))
448 if (!((LEGACY_UART_CLOCK
/ 16) % new_uart_baud_rate
)) {
449 uart_baud_rate
= new_uart_baud_rate
;
453 if ( mmio_uart_probe() )
455 gPESF
= &mmio_uart_serial_functions
;
457 lpss_uart_supported
= 1;
458 lpss_uart_enabled
= 1;
461 else if ( legacy_uart_probe() )
463 gPESF
= &legacy_uart_serial_functions
;
465 legacy_uart_enabled
= 1;
478 if (uart_initted
&& (legacy_uart_enabled
|| lpss_uart_enabled
)) {
479 while (!gPESF
->tr0()); /* Wait until THR is empty. */
487 if (uart_initted
&& (legacy_uart_enabled
|| lpss_uart_enabled
)) {
496 serial_putc( char c
)