Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | // SPDX-License-Identifier: GPL-2.0+ /* * Copyright (c) 2015 Google, Inc * Written by Simon Glass <sjg@chromium.org> */ #include <debug_uart.h> #include <dm.h> #include <efi.h> #include <efi_api.h> #include <errno.h> #include <fdtdec.h> #include <log.h> #include <linux/compiler.h> #include <asm/global_data.h> #include <asm/io.h> #include <serial.h> DECLARE_GLOBAL_DATA_PTR; /* Information about the efi console */ struct serial_efi_priv { struct efi_simple_text_input_protocol *con_in; struct efi_simple_text_output_protocol *con_out; struct efi_boot_services *boot; struct efi_input_key key; struct efi_event *timer; bool have_key; }; /* Convert a lower-case character to its ctrl-char equivalent */ #define CTL_CH(c) ((c) - 'a' + 1) int serial_efi_setbrg(struct udevice *dev, int baudrate) { return 0; } /** * serial_efi_get_key() - Read a keystroke, waiting up to 1ms * * Checks for a key, while allowing the EFI system to process other events. * * Return: 0 if a keystroke was read, -EAGAIN if there is no keystroke yet, -EIO * if reading the keystroke failed */ static int serial_efi_get_key(struct serial_efi_priv *priv) { struct efi_simple_text_input_protocol *cin = priv->con_in; efi_status_t ret; if (priv->have_key) return 0; /* For emulators like QEMU, skip the timer wait since USB event * processing is not needed */ if (gd->flags & GD_FLG_EMUL) { ret = priv->con_in->read_key_stroke(priv->con_in, &priv->key); if (ret == EFI_NOT_READY) return -EAGAIN; else if (ret != EFI_SUCCESS) return -EIO; } else { struct efi_event *events[2] = {cin->wait_for_key, priv->timer}; efi_uintn_t index; ret = priv->boot->wait_for_event(2, events, &index); if (ret) { log_err("wait_for_event() failed\n"); return -EAGAIN; } if (index) return -EAGAIN; ret = priv->con_in->read_key_stroke(priv->con_in, &priv->key); if (ret == EFI_NOT_READY) return -EAGAIN; else if (ret != EFI_SUCCESS) return -EIO; } priv->have_key = true; return 0; } static int serial_efi_getc(struct udevice *dev) { struct serial_efi_priv *priv = dev_get_priv(dev); int ret, ch; ret = serial_efi_get_key(priv); if (ret) return ret; priv->have_key = false; ch = efi_decode_key(&priv->key); return ch; } static int serial_efi_putc(struct udevice *dev, const char ch) { struct serial_efi_priv *priv = dev_get_priv(dev); uint16_t ucode[2]; int ret; ucode[0] = ch; ucode[1] = '\0'; ret = priv->con_out->output_string(priv->con_out, ucode); if (ret) return -EIO; return 0; } static int serial_efi_pending(struct udevice *dev, bool input) { struct serial_efi_priv *priv = dev_get_priv(dev); int ret; /* We assume that EFI will stall if its output buffer fills up */ if (!input) return 0; ret = serial_efi_get_key(priv); if (ret == -EAGAIN) return 0; else if (ret) return ret; return 1; } /* * There is nothing to init here since the EFI console is already running by * the time we enter U-Boot. */ static inline void _debug_uart_init(void) { } static inline void _debug_uart_putc(int ch) { struct efi_system_table *sys_table = efi_get_sys_table(); uint16_t ucode[2]; ucode[0] = ch; ucode[1] = '\0'; sys_table->con_out->output_string(sys_table->con_out, ucode); } DEBUG_UART_FUNCS static int serial_efi_probe(struct udevice *dev) { struct efi_system_table *table = efi_get_sys_table(); struct serial_efi_priv *priv = dev_get_priv(dev); struct efi_boot_services *boot = efi_get_boot(); efi_status_t ret; priv->con_in = table->con_in; priv->con_out = table->con_out; priv->con_in->reset(priv->con_in, true); priv->boot = boot; ret = boot->create_event(EVT_TIMER, TPL_APPLICATION, NULL, NULL, &priv->timer); if (ret) return -ECOMM; ret = boot->set_timer(priv->timer, EFI_TIMER_PERIODIC, 100 /* 10μs in 100ns units */); if (ret) return -ECOMM; return 0; } static const struct dm_serial_ops serial_efi_ops = { .putc = serial_efi_putc, .getc = serial_efi_getc, .pending = serial_efi_pending, .setbrg = serial_efi_setbrg, }; static const struct udevice_id serial_efi_ids[] = { { .compatible = "efi,uart" }, { } }; U_BOOT_DRIVER(serial_efi) = { .name = "serial_efi", .id = UCLASS_SERIAL, .of_match = serial_efi_ids, .priv_auto = sizeof(struct serial_efi_priv), .probe = serial_efi_probe, .ops = &serial_efi_ops, }; |