2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/serial.h>
20 #include <grub/ns8250.h>
21 #include <grub/types.h>
23 #include <grub/misc.h>
24 #include <grub/cpu/io.h>
26 #include <grub/time.h>
27 #include <grub/i18n.h>
29 #ifdef GRUB_MACHINE_PCBIOS
30 #include <grub/machine/memory.h>
31 #define GRUB_SERIAL_PORT_NUM 4
33 #include <grub/machine/serial.h>
34 static const grub_port_t serial_hw_io_addr
[] = GRUB_MACHINE_SERIAL_PORTS
;
35 #define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
38 static int dead_ports
= 0;
41 ns8250_reg_read (struct grub_serial_port
*port
, grub_addr_t reg
)
43 asm volatile("" : : : "memory");
44 if (port
->use_mmio
== true)
47 * Note: we assume MMIO UARTs are little endian. This is not true of all
48 * embedded platforms but will do for now.
50 switch(port
->mmio
.access_size
)
53 /* ACPI tables occasionally uses "0" (legacy) as equivalent to "1" (byte). */
55 return *((volatile grub_uint8_t
*) (port
->mmio
.base
+ reg
));
57 return grub_le_to_cpu16 (*((volatile grub_uint16_t
*) (port
->mmio
.base
+ (reg
<< 1))));
59 return grub_le_to_cpu32 (*((volatile grub_uint32_t
*) (port
->mmio
.base
+ (reg
<< 2))));
62 * This will only work properly on 64-bit systems since 64-bit
63 * accessors aren't atomic on 32-bit hardware. Thankfully the
64 * case of a UART with a 64-bit register spacing on 32-bit
65 * also probably doesn't exist.
67 return grub_le_to_cpu64 (*((volatile grub_uint64_t
*) (port
->mmio
.base
+ (reg
<< 3))));
70 return grub_inb (port
->port
+ reg
);
74 ns8250_reg_write (struct grub_serial_port
*port
, grub_uint8_t value
, grub_addr_t reg
)
76 asm volatile("" : : : "memory");
77 if (port
->use_mmio
== true)
79 switch(port
->mmio
.access_size
)
82 /* ACPI tables occasionally uses "0" (legacy) as equivalent to "1" (byte). */
84 *((volatile grub_uint8_t
*) (port
->mmio
.base
+ reg
)) = value
;
87 *((volatile grub_uint16_t
*) (port
->mmio
.base
+ (reg
<< 1))) = grub_cpu_to_le16 (value
);
90 *((volatile grub_uint32_t
*) (port
->mmio
.base
+ (reg
<< 2))) = grub_cpu_to_le32 (value
);
93 /* See commment in ns8250_reg_read(). */
94 *((volatile grub_uint64_t
*) (port
->mmio
.base
+ (reg
<< 3))) = grub_cpu_to_le64 (value
);
99 grub_outb (value
, port
->port
+ reg
);
102 /* Convert speed to divisor. */
103 static unsigned short
104 serial_get_divisor (const struct grub_serial_port
*port
__attribute__ ((unused
)),
105 const struct grub_serial_config
*config
)
107 grub_uint32_t base_clock
;
108 grub_uint32_t divisor
;
109 grub_uint32_t actual_speed
, error
;
111 /* Get the UART input clock frequency. */
112 base_clock
= config
->base_clock
? config
->base_clock
: UART_DEFAULT_BASE_CLOCK
;
115 * The UART uses 16 times oversampling for the BRG, so adjust the value
116 * accordingly to calculate the divisor.
120 divisor
= (base_clock
+ (config
->speed
/ 2)) / config
->speed
;
121 if (config
->speed
== 0)
123 if (divisor
> 0xffff || divisor
== 0)
125 actual_speed
= base_clock
/ divisor
;
126 error
= actual_speed
> config
->speed
? (actual_speed
- config
->speed
)
127 : (config
->speed
- actual_speed
);
128 if (error
> (config
->speed
/ 30 + 1))
134 do_real_config (struct grub_serial_port
*port
)
137 unsigned char status
= 0;
138 grub_uint64_t endtime
;
140 const unsigned char parities
[] = {
141 [GRUB_SERIAL_PARITY_NONE
] = UART_NO_PARITY
,
142 [GRUB_SERIAL_PARITY_ODD
] = UART_ODD_PARITY
,
143 [GRUB_SERIAL_PARITY_EVEN
] = UART_EVEN_PARITY
145 const unsigned char stop_bits
[] = {
146 [GRUB_SERIAL_STOP_BITS_1
] = UART_1_STOP_BIT
,
147 [GRUB_SERIAL_STOP_BITS_2
] = UART_2_STOP_BITS
,
150 if (port
->configured
)
155 divisor
= serial_get_divisor (port
, &port
->config
);
157 /* Turn off the interrupt. */
158 ns8250_reg_write (port
, 0, UART_IER
);
161 ns8250_reg_write (port
, UART_DLAB
, UART_LCR
);
163 ns8250_reg_write (port
, divisor
& 0xFF, UART_DLL
);
164 ns8250_reg_write (port
, divisor
>> 8, UART_DLH
);
166 /* Set the line status. */
167 status
|= (parities
[port
->config
.parity
]
168 | (port
->config
.word_len
- 5)
169 | stop_bits
[port
->config
.stop_bits
]);
170 ns8250_reg_write (port
, status
, UART_LCR
);
172 if (port
->config
.rtscts
)
174 /* Enable the FIFO. */
175 ns8250_reg_write (port
, UART_ENABLE_FIFO_TRIGGER1
, UART_FCR
);
177 /* Turn on DTR and RTS. */
178 ns8250_reg_write (port
, UART_ENABLE_DTRRTS
, UART_MCR
);
182 /* Enable the FIFO. */
183 ns8250_reg_write (port
, UART_ENABLE_FIFO_TRIGGER14
, UART_FCR
);
185 /* Turn on DTR, RTS, and OUT2. */
186 ns8250_reg_write (port
, UART_ENABLE_DTRRTS
| UART_ENABLE_OUT2
, UART_MCR
);
189 /* Drain the input buffer. */
190 endtime
= grub_get_time_ms () + 1000;
191 while (ns8250_reg_read (port
, UART_LSR
) & UART_DATA_READY
)
193 ns8250_reg_read (port
, UART_RX
);
194 if (grub_get_time_ms () > endtime
)
201 port
->configured
= 1;
206 serial_hw_fetch (struct grub_serial_port
*port
)
208 do_real_config (port
);
209 if (ns8250_reg_read (port
, UART_LSR
) & UART_DATA_READY
)
210 return ns8250_reg_read (port
, UART_RX
);
215 /* Put a character. */
217 serial_hw_put (struct grub_serial_port
*port
, const int c
)
219 grub_uint64_t endtime
;
221 do_real_config (port
);
223 if (port
->broken
> 5)
224 endtime
= grub_get_time_ms ();
225 else if (port
->broken
> 1)
226 endtime
= grub_get_time_ms () + 50;
228 endtime
= grub_get_time_ms () + 200;
229 /* Wait until the transmitter holding register is empty. */
230 while ((ns8250_reg_read (port
, UART_LSR
) & UART_EMPTY_TRANSMITTER
) == 0)
232 if (grub_get_time_ms () > endtime
)
235 /* There is something wrong. But what can I do? */
243 ns8250_reg_write (port
, c
, UART_TX
);
246 /* Initialize a serial device. PORT is the port number for a serial device.
247 SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
248 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
249 for the device. Likewise, PARITY is the type of the parity and
250 STOP_BIT_LEN is the length of the stop bit. The possible values for
251 WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
254 serial_hw_configure (struct grub_serial_port
*port
,
255 struct grub_serial_config
*config
)
257 unsigned short divisor
;
259 divisor
= serial_get_divisor (port
, config
);
261 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
262 N_("unsupported serial port speed"));
264 if (config
->parity
!= GRUB_SERIAL_PARITY_NONE
265 && config
->parity
!= GRUB_SERIAL_PARITY_ODD
266 && config
->parity
!= GRUB_SERIAL_PARITY_EVEN
)
267 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
268 N_("unsupported serial port parity"));
270 if (config
->stop_bits
!= GRUB_SERIAL_STOP_BITS_1
271 && config
->stop_bits
!= GRUB_SERIAL_STOP_BITS_2
)
272 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
273 N_("unsupported serial port stop bits number"));
275 if (config
->word_len
< 5 || config
->word_len
> 8)
276 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
277 N_("unsupported serial port word length"));
279 port
->config
= *config
;
280 port
->configured
= 0;
282 /* FIXME: should check if the serial terminal was found. */
284 return GRUB_ERR_NONE
;
287 struct grub_serial_driver grub_ns8250_driver
=
289 .configure
= serial_hw_configure
,
290 .fetch
= serial_hw_fetch
,
294 static char com_names
[GRUB_SERIAL_PORT_NUM
][20];
295 static struct grub_serial_port com_ports
[GRUB_SERIAL_PORT_NUM
];
298 grub_ns8250_init (void)
300 #ifdef GRUB_MACHINE_PCBIOS
301 const unsigned short *serial_hw_io_addr
= (const unsigned short *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR
);
304 for (i
= 0; i
< GRUB_SERIAL_PORT_NUM
; i
++)
305 if (serial_hw_io_addr
[i
])
309 grub_outb (0x5a, serial_hw_io_addr
[i
] + UART_SR
);
310 if (grub_inb (serial_hw_io_addr
[i
] + UART_SR
) != 0x5a)
312 dead_ports
|= (1 << i
);
315 grub_outb (0xa5, serial_hw_io_addr
[i
] + UART_SR
);
316 if (grub_inb (serial_hw_io_addr
[i
] + UART_SR
) != 0xa5)
318 dead_ports
|= (1 << i
);
322 grub_snprintf (com_names
[i
], sizeof (com_names
[i
]), "com%d", i
);
323 com_ports
[i
].name
= com_names
[i
];
324 com_ports
[i
].driver
= &grub_ns8250_driver
;
325 com_ports
[i
].port
= serial_hw_io_addr
[i
];
326 com_ports
[i
].use_mmio
= false;
327 err
= grub_serial_config_defaults (&com_ports
[i
]);
331 grub_serial_register (&com_ports
[i
]);
335 /* Return the port number for the UNITth serial device. */
337 grub_ns8250_hw_get_port (const unsigned int unit
)
339 #ifdef GRUB_MACHINE_PCBIOS
340 const unsigned short *serial_hw_io_addr
= (const unsigned short *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR
);
342 if (unit
< GRUB_SERIAL_PORT_NUM
343 && !(dead_ports
& (1 << unit
)))
344 return serial_hw_io_addr
[unit
];
349 struct grub_serial_port
*
350 grub_serial_ns8250_add_port (grub_port_t port
, struct grub_serial_config
*config
)
352 struct grub_serial_port
*p
;
355 for (i
= 0; i
< GRUB_SERIAL_PORT_NUM
; i
++)
356 if (com_ports
[i
].port
== port
)
358 if (dead_ports
& (1 << i
))
360 /* Give the opportunity for SPCR to configure a default com port. */
362 grub_serial_port_configure (&com_ports
[i
], config
);
363 return &com_ports
[i
];
367 if (p
->use_mmio
== false && p
->port
== port
)
370 grub_serial_port_configure (p
, config
);
374 grub_outb (0x5a, port
+ UART_SR
);
375 if (grub_inb (port
+ UART_SR
) != 0x5a)
378 grub_outb (0xa5, port
+ UART_SR
);
379 if (grub_inb (port
+ UART_SR
) != 0xa5)
382 p
= grub_malloc (sizeof (*p
));
385 p
->name
= grub_xasprintf ("port%lx", (unsigned long) port
);
391 p
->driver
= &grub_ns8250_driver
;
395 grub_serial_port_configure (p
, config
);
397 grub_serial_config_defaults (p
);
398 grub_serial_register (p
);
403 struct grub_serial_port
*
404 grub_serial_ns8250_add_mmio (grub_addr_t addr
, unsigned int acc_size
,
405 struct grub_serial_config
*config
)
407 struct grub_serial_port
*p
;
410 for (i
= 0; i
< GRUB_SERIAL_PORT_NUM
; i
++)
411 if (com_ports
[i
].use_mmio
== true && com_ports
[i
].mmio
.base
== addr
)
414 grub_serial_port_configure (&com_ports
[i
], config
);
415 return &com_ports
[i
];
419 if (p
->use_mmio
== true && p
->mmio
.base
== addr
)
422 grub_serial_port_configure (p
, config
);
426 p
= grub_malloc (sizeof (*p
));
429 p
->name
= grub_xasprintf ("mmio,%llx", (unsigned long long) addr
);
435 p
->driver
= &grub_ns8250_driver
;
438 p
->mmio
.access_size
= acc_size
;
440 grub_serial_port_configure (p
, config
);
442 grub_serial_config_defaults (p
);
443 grub_serial_register (p
);