tpm2_key_protector: Enable build for powerpc_ieee1275
[grub.git] / grub-core / term / ns8250.c
blob23e8e09048a78faf73a00b62ca2c182b28cbdc62
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 #define GRUB_SERIAL_PORT_NUM 4
32 #else
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))
36 #endif
38 static int dead_ports = 0;
40 static grub_uint8_t
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)
52 default:
53 /* ACPI tables occasionally uses "0" (legacy) as equivalent to "1" (byte). */
54 case 1:
55 return *((volatile grub_uint8_t *) (port->mmio.base + reg));
56 case 2:
57 return grub_le_to_cpu16 (*((volatile grub_uint16_t *) (port->mmio.base + (reg << 1))));
58 case 3:
59 return grub_le_to_cpu32 (*((volatile grub_uint32_t *) (port->mmio.base + (reg << 2))));
60 case 4:
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);
73 static void
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)
81 default:
82 /* ACPI tables occasionally uses "0" (legacy) as equivalent to "1" (byte). */
83 case 1:
84 *((volatile grub_uint8_t *) (port->mmio.base + reg)) = value;
85 break;
86 case 2:
87 *((volatile grub_uint16_t *) (port->mmio.base + (reg << 1))) = grub_cpu_to_le16 (value);
88 break;
89 case 3:
90 *((volatile grub_uint32_t *) (port->mmio.base + (reg << 2))) = grub_cpu_to_le32 (value);
91 break;
92 case 4:
93 /* See commment in ns8250_reg_read(). */
94 *((volatile grub_uint64_t *) (port->mmio.base + (reg << 3))) = grub_cpu_to_le64 (value);
95 break;
98 else
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.
118 base_clock >>= 4;
120 divisor = (base_clock + (config->speed / 2)) / config->speed;
121 if (config->speed == 0)
122 return 0;
123 if (divisor > 0xffff || divisor == 0)
124 return 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))
129 return 0;
130 return divisor;
133 static void
134 do_real_config (struct grub_serial_port *port)
136 int divisor;
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)
151 return;
153 port->broken = 0;
155 divisor = serial_get_divisor (port, &port->config);
157 /* Turn off the interrupt. */
158 ns8250_reg_write (port, 0, UART_IER);
160 /* Set DLAB. */
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);
180 else
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)
196 port->broken = 1;
197 break;
201 port->configured = 1;
204 /* Fetch a key. */
205 static int
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);
212 return -1;
215 /* Put a character. */
216 static void
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;
227 else
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)
234 port->broken++;
235 /* There is something wrong. But what can I do? */
236 return;
240 if (port->broken)
241 port->broken--;
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
252 macros. */
253 static grub_err_t
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);
260 if (divisor == 0)
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,
291 .put = serial_hw_put
294 static char com_names[GRUB_SERIAL_PORT_NUM][20];
295 static struct grub_serial_port com_ports[GRUB_SERIAL_PORT_NUM];
297 void
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);
302 #endif
303 unsigned i;
304 for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
305 if (serial_hw_io_addr[i])
307 grub_err_t err;
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);
313 continue;
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);
319 continue;
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]);
328 if (err)
329 grub_print_error ();
331 grub_serial_register (&com_ports[i]);
335 /* Return the port number for the UNITth serial device. */
336 grub_port_t
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);
341 #endif
342 if (unit < GRUB_SERIAL_PORT_NUM
343 && !(dead_ports & (1 << unit)))
344 return serial_hw_io_addr[unit];
345 else
346 return 0;
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;
353 unsigned i;
355 for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
356 if (com_ports[i].port == port)
358 if (dead_ports & (1 << i))
359 return NULL;
360 /* Give the opportunity for SPCR to configure a default com port. */
361 if (config != NULL)
362 grub_serial_port_configure (&com_ports[i], config);
363 return &com_ports[i];
366 FOR_SERIAL_PORTS (p)
367 if (p->use_mmio == false && p->port == port)
369 if (config != NULL)
370 grub_serial_port_configure (p, config);
371 return p;
374 grub_outb (0x5a, port + UART_SR);
375 if (grub_inb (port + UART_SR) != 0x5a)
376 return NULL;
378 grub_outb (0xa5, port + UART_SR);
379 if (grub_inb (port + UART_SR) != 0xa5)
380 return NULL;
382 p = grub_malloc (sizeof (*p));
383 if (p == NULL)
384 return NULL;
385 p->name = grub_xasprintf ("port%lx", (unsigned long) port);
386 if (p->name == NULL)
388 grub_free (p);
389 return NULL;
391 p->driver = &grub_ns8250_driver;
392 p->use_mmio = false;
393 p->port = port;
394 if (config != NULL)
395 grub_serial_port_configure (p, config);
396 else
397 grub_serial_config_defaults (p);
398 grub_serial_register (p);
400 return 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;
408 unsigned i;
410 for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
411 if (com_ports[i].use_mmio == true && com_ports[i].mmio.base == addr)
413 if (config != NULL)
414 grub_serial_port_configure (&com_ports[i], config);
415 return &com_ports[i];
418 FOR_SERIAL_PORTS (p)
419 if (p->use_mmio == true && p->mmio.base == addr)
421 if (config != NULL)
422 grub_serial_port_configure (p, config);
423 return p;
426 p = grub_malloc (sizeof (*p));
427 if (p == NULL)
428 return NULL;
429 p->name = grub_xasprintf ("mmio,%llx", (unsigned long long) addr);
430 if (p->name == NULL)
432 grub_free (p);
433 return NULL;
435 p->driver = &grub_ns8250_driver;
436 p->use_mmio = true;
437 p->mmio.base = addr;
438 p->mmio.access_size = acc_size;
439 if (config != NULL)
440 grub_serial_port_configure (p, config);
441 else
442 grub_serial_config_defaults (p);
443 grub_serial_register (p);
445 return p;