Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / term / ns8250.c
blob39809d0423b22ac5d3b97159d64a6ba0a0dcf368
1 /*
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>
22 #include <grub/dl.h>
23 #include <grub/misc.h>
24 #include <grub/cpu/io.h>
25 #include <grub/mm.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
33 #else
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))
37 #endif
39 static int dead_ports = 0;
41 #ifdef GRUB_MACHINE_MIPS_LOONGSON
42 #define DEFAULT_BASE_CLOCK (2 * 115200)
43 #else
44 #define DEFAULT_BASE_CLOCK 115200
45 #endif
48 /* Convert speed to divisor. */
49 static unsigned short
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)
61 return 0;
62 if (divisor > 0xffff || divisor == 0)
63 return 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))
68 return 0;
69 return divisor;
72 static void
73 do_real_config (struct grub_serial_port *port)
75 int divisor;
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,
89 if (port->configured)
90 return;
92 port->broken = 0;
94 divisor = serial_get_divisor (port, &port->config);
96 /* Turn off the interrupt. */
97 grub_outb (0, port->port + UART_IER);
99 /* Set DLAB. */
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);
120 else
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)
136 port->broken = 1;
137 break;
141 port->configured = 1;
144 /* Fetch a key. */
145 static int
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);
152 return -1;
155 /* Put a character. */
156 static void
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;
167 else
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)
174 port->broken++;
175 /* There is something wrong. But what can I do? */
176 return;
180 if (port->broken)
181 port->broken--;
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
192 macros. */
193 static grub_err_t
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);
200 if (divisor == 0)
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,
231 .put = serial_hw_put
234 static char com_names[GRUB_SERIAL_PORT_NUM][20];
235 static struct grub_serial_port com_ports[GRUB_SERIAL_PORT_NUM];
237 void
238 grub_ns8250_init (void)
240 unsigned i;
241 for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
242 if (serial_hw_io_addr[i])
244 grub_err_t err;
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);
250 continue;
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);
256 continue;
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]);
264 if (err)
265 grub_print_error ();
267 grub_serial_register (&com_ports[i]);
271 /* Return the port number for the UNITth serial device. */
272 grub_port_t
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];
278 else
279 return 0;
282 char *
283 grub_serial_ns8250_add_port (grub_port_t port)
285 struct grub_serial_port *p;
286 unsigned i;
287 for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
288 if (com_ports[i].port == port)
290 if (dead_ports & (1 << i))
291 return NULL;
292 return com_names[i];
295 grub_outb (0x5a, port + UART_SR);
296 if (grub_inb (port + UART_SR) != 0x5a)
297 return NULL;
299 grub_outb (0xa5, port + UART_SR);
300 if (grub_inb (port + UART_SR) != 0xa5)
301 return NULL;
303 p = grub_malloc (sizeof (*p));
304 if (!p)
305 return NULL;
306 p->name = grub_xasprintf ("port%lx", (unsigned long) port);
307 if (!p->name)
309 grub_free (p);
310 return NULL;
312 p->driver = &grub_ns8250_driver;
313 grub_serial_config_defaults (p);
314 p->port = port;
315 grub_serial_register (p);
317 return p->name;