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 static const unsigned short *serial_hw_io_addr
= (const unsigned short *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR
;
32 #define GRUB_SERIAL_PORT_NUM 4
34 #include <grub/machine/serial.h>
35 static const grub_port_t serial_hw_io_addr
[] = GRUB_MACHINE_SERIAL_PORTS
;
36 #define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
39 static int dead_ports
= 0;
41 #ifdef GRUB_MACHINE_MIPS_LOONGSON
42 #define DEFAULT_BASE_CLOCK (2 * 115200)
44 #define DEFAULT_BASE_CLOCK 115200
48 /* Convert speed to divisor. */
50 serial_get_divisor (const struct grub_serial_port
*port
__attribute__ ((unused
)),
51 const struct grub_serial_config
*config
)
53 grub_uint32_t base_clock
;
54 grub_uint32_t divisor
;
55 grub_uint32_t actual_speed
, error
;
57 base_clock
= config
->base_clock
? (config
->base_clock
>> 4) : DEFAULT_BASE_CLOCK
;
59 divisor
= (base_clock
+ (config
->speed
/ 2)) / config
->speed
;
60 if (config
->speed
== 0)
62 if (divisor
> 0xffff || divisor
== 0)
64 actual_speed
= base_clock
/ divisor
;
65 error
= actual_speed
> config
->speed
? (actual_speed
- config
->speed
)
66 : (config
->speed
- actual_speed
);
67 if (error
> (config
->speed
/ 30 + 1))
73 do_real_config (struct grub_serial_port
*port
)
76 unsigned char status
= 0;
77 grub_uint64_t endtime
;
79 const unsigned char parities
[] = {
80 [GRUB_SERIAL_PARITY_NONE
] = UART_NO_PARITY
,
81 [GRUB_SERIAL_PARITY_ODD
] = UART_ODD_PARITY
,
82 [GRUB_SERIAL_PARITY_EVEN
] = UART_EVEN_PARITY
84 const unsigned char stop_bits
[] = {
85 [GRUB_SERIAL_STOP_BITS_1
] = UART_1_STOP_BIT
,
86 [GRUB_SERIAL_STOP_BITS_2
] = UART_2_STOP_BITS
,
94 divisor
= serial_get_divisor (port
, &port
->config
);
96 /* Turn off the interrupt. */
97 grub_outb (0, port
->port
+ UART_IER
);
100 grub_outb (UART_DLAB
, port
->port
+ UART_LCR
);
102 /* Set the baud rate. */
103 grub_outb (divisor
& 0xFF, port
->port
+ UART_DLL
);
104 grub_outb (divisor
>> 8, port
->port
+ UART_DLH
);
106 /* Set the line status. */
107 status
|= (parities
[port
->config
.parity
]
108 | (port
->config
.word_len
- 5)
109 | stop_bits
[port
->config
.stop_bits
]);
110 grub_outb (status
, port
->port
+ UART_LCR
);
112 if (port
->config
.rtscts
)
114 /* Enable the FIFO. */
115 grub_outb (UART_ENABLE_FIFO_TRIGGER1
, port
->port
+ UART_FCR
);
117 /* Turn on DTR and RTS. */
118 grub_outb (UART_ENABLE_DTRRTS
, port
->port
+ UART_MCR
);
122 /* Enable the FIFO. */
123 grub_outb (UART_ENABLE_FIFO_TRIGGER14
, port
->port
+ UART_FCR
);
125 /* Turn on DTR, RTS, and OUT2. */
126 grub_outb (UART_ENABLE_DTRRTS
| UART_ENABLE_OUT2
, port
->port
+ UART_MCR
);
129 /* Drain the input buffer. */
130 endtime
= grub_get_time_ms () + 1000;
131 while (grub_inb (port
->port
+ UART_LSR
) & UART_DATA_READY
)
133 grub_inb (port
->port
+ UART_RX
);
134 if (grub_get_time_ms () > endtime
)
141 port
->configured
= 1;
146 serial_hw_fetch (struct grub_serial_port
*port
)
148 do_real_config (port
);
149 if (grub_inb (port
->port
+ UART_LSR
) & UART_DATA_READY
)
150 return grub_inb (port
->port
+ UART_RX
);
155 /* Put a character. */
157 serial_hw_put (struct grub_serial_port
*port
, const int c
)
159 grub_uint64_t endtime
;
161 do_real_config (port
);
163 if (port
->broken
> 5)
164 endtime
= grub_get_time_ms ();
165 else if (port
->broken
> 1)
166 endtime
= grub_get_time_ms () + 50;
168 endtime
= grub_get_time_ms () + 200;
169 /* Wait until the transmitter holding register is empty. */
170 while ((grub_inb (port
->port
+ UART_LSR
) & UART_EMPTY_TRANSMITTER
) == 0)
172 if (grub_get_time_ms () > endtime
)
175 /* There is something wrong. But what can I do? */
183 grub_outb (c
, port
->port
+ UART_TX
);
186 /* Initialize a serial device. PORT is the port number for a serial device.
187 SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
188 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
189 for the device. Likewise, PARITY is the type of the parity and
190 STOP_BIT_LEN is the length of the stop bit. The possible values for
191 WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
194 serial_hw_configure (struct grub_serial_port
*port
,
195 struct grub_serial_config
*config
)
197 unsigned short divisor
;
199 divisor
= serial_get_divisor (port
, config
);
201 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
202 N_("unsupported serial port speed"));
204 if (config
->parity
!= GRUB_SERIAL_PARITY_NONE
205 && config
->parity
!= GRUB_SERIAL_PARITY_ODD
206 && config
->parity
!= GRUB_SERIAL_PARITY_EVEN
)
207 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
208 N_("unsupported serial port parity"));
210 if (config
->stop_bits
!= GRUB_SERIAL_STOP_BITS_1
211 && config
->stop_bits
!= GRUB_SERIAL_STOP_BITS_2
)
212 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
213 N_("unsupported serial port stop bits number"));
215 if (config
->word_len
< 5 || config
->word_len
> 8)
216 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
217 N_("unsupported serial port word length"));
219 port
->config
= *config
;
220 port
->configured
= 0;
222 /* FIXME: should check if the serial terminal was found. */
224 return GRUB_ERR_NONE
;
227 struct grub_serial_driver grub_ns8250_driver
=
229 .configure
= serial_hw_configure
,
230 .fetch
= serial_hw_fetch
,
234 static char com_names
[GRUB_SERIAL_PORT_NUM
][20];
235 static struct grub_serial_port com_ports
[GRUB_SERIAL_PORT_NUM
];
238 grub_ns8250_init (void)
241 for (i
= 0; i
< GRUB_SERIAL_PORT_NUM
; i
++)
242 if (serial_hw_io_addr
[i
])
246 grub_outb (0x5a, serial_hw_io_addr
[i
] + UART_SR
);
247 if (grub_inb (serial_hw_io_addr
[i
] + UART_SR
) != 0x5a)
249 dead_ports
|= (1 << i
);
252 grub_outb (0xa5, serial_hw_io_addr
[i
] + UART_SR
);
253 if (grub_inb (serial_hw_io_addr
[i
] + UART_SR
) != 0xa5)
255 dead_ports
|= (1 << i
);
259 grub_snprintf (com_names
[i
], sizeof (com_names
[i
]), "com%d", i
);
260 com_ports
[i
].name
= com_names
[i
];
261 com_ports
[i
].driver
= &grub_ns8250_driver
;
262 com_ports
[i
].port
= serial_hw_io_addr
[i
];
263 err
= grub_serial_config_defaults (&com_ports
[i
]);
267 grub_serial_register (&com_ports
[i
]);
271 /* Return the port number for the UNITth serial device. */
273 grub_ns8250_hw_get_port (const unsigned int unit
)
275 if (unit
< GRUB_SERIAL_PORT_NUM
276 && !(dead_ports
& (1 << unit
)))
277 return serial_hw_io_addr
[unit
];
283 grub_serial_ns8250_add_port (grub_port_t port
)
285 struct grub_serial_port
*p
;
287 for (i
= 0; i
< GRUB_SERIAL_PORT_NUM
; i
++)
288 if (com_ports
[i
].port
== port
)
290 if (dead_ports
& (1 << i
))
295 grub_outb (0x5a, port
+ UART_SR
);
296 if (grub_inb (port
+ UART_SR
) != 0x5a)
299 grub_outb (0xa5, port
+ UART_SR
);
300 if (grub_inb (port
+ UART_SR
) != 0xa5)
303 p
= grub_malloc (sizeof (*p
));
306 p
->name
= grub_xasprintf ("port%lx", (unsigned long) port
);
312 p
->driver
= &grub_ns8250_driver
;
313 grub_serial_config_defaults (p
);
315 grub_serial_register (p
);