| 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * IBM RTAS driver interface to hvc_console.c |
| 4 | * |
| 5 | * (C) Copyright IBM Corporation 2001-2005 |
| 6 | * (C) Copyright Red Hat, Inc. 2005 |
| 7 | * |
| 8 | * Author(s): Maximino Augilar <IBM STI Design Center> |
| 9 | * : Ryan S. Arnold <rsa@us.ibm.com> |
| 10 | * : Utz Bacher <utz.bacher@de.ibm.com> |
| 11 | * : David Woodhouse <dwmw2@infradead.org> |
| 12 | * |
| 13 | * inspired by drivers/char/hvc_console.c |
| 14 | * written by Anton Blanchard and Paul Mackerras |
| 15 | */ |
| 16 | |
| 17 | #include <linux/console.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/moduleparam.h> |
| 22 | #include <linux/types.h> |
| 23 | |
| 24 | #include <asm/irq.h> |
| 25 | #include <asm/rtas.h> |
| 26 | #include "hvc_console.h" |
| 27 | |
| 28 | #define hvc_rtas_cookie 0x67781e15 |
| 29 | static struct hvc_struct *hvc_rtas_dev; |
| 30 | |
| 31 | static int rtascons_put_char_token = RTAS_UNKNOWN_SERVICE; |
| 32 | static int rtascons_get_char_token = RTAS_UNKNOWN_SERVICE; |
| 33 | |
| 34 | static ssize_t hvc_rtas_write_console(uint32_t vtermno, const u8 *buf, |
| 35 | size_t count) |
| 36 | { |
| 37 | size_t i; |
| 38 | |
| 39 | for (i = 0; i < count; i++) { |
| 40 | if (rtas_call(rtascons_put_char_token, 1, 1, NULL, buf[i])) |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | return i; |
| 45 | } |
| 46 | |
| 47 | static ssize_t hvc_rtas_read_console(uint32_t vtermno, u8 *buf, size_t count) |
| 48 | { |
| 49 | size_t i; |
| 50 | int c; |
| 51 | |
| 52 | for (i = 0; i < count; i++) { |
| 53 | if (rtas_call(rtascons_get_char_token, 0, 2, &c)) |
| 54 | break; |
| 55 | |
| 56 | buf[i] = c; |
| 57 | } |
| 58 | |
| 59 | return i; |
| 60 | } |
| 61 | |
| 62 | static const struct hv_ops hvc_rtas_get_put_ops = { |
| 63 | .get_chars = hvc_rtas_read_console, |
| 64 | .put_chars = hvc_rtas_write_console, |
| 65 | }; |
| 66 | |
| 67 | static int __init hvc_rtas_init(void) |
| 68 | { |
| 69 | struct hvc_struct *hp; |
| 70 | |
| 71 | if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE) |
| 72 | rtascons_put_char_token = rtas_token("put-term-char" ); |
| 73 | if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE) |
| 74 | return -EIO; |
| 75 | |
| 76 | if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE) |
| 77 | rtascons_get_char_token = rtas_token("get-term-char" ); |
| 78 | if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE) |
| 79 | return -EIO; |
| 80 | |
| 81 | BUG_ON(hvc_rtas_dev); |
| 82 | |
| 83 | /* Allocate an hvc_struct for the console device we instantiated |
| 84 | * earlier. Save off hp so that we can return it on exit */ |
| 85 | hp = hvc_alloc(hvc_rtas_cookie, data: 0, ops: &hvc_rtas_get_put_ops, outbuf_size: 16); |
| 86 | if (IS_ERR(ptr: hp)) |
| 87 | return PTR_ERR(ptr: hp); |
| 88 | |
| 89 | hvc_rtas_dev = hp; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | device_initcall(hvc_rtas_init); |
| 94 | |
| 95 | /* This will happen prior to module init. There is no tty at this time? */ |
| 96 | static int __init hvc_rtas_console_init(void) |
| 97 | { |
| 98 | rtascons_put_char_token = rtas_token("put-term-char" ); |
| 99 | if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE) |
| 100 | return -EIO; |
| 101 | |
| 102 | rtascons_get_char_token = rtas_token("get-term-char" ); |
| 103 | if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE) |
| 104 | return -EIO; |
| 105 | |
| 106 | hvc_instantiate(hvc_rtas_cookie, index: 0, ops: &hvc_rtas_get_put_ops); |
| 107 | add_preferred_console(name: "hvc" , idx: 0, NULL); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | console_initcall(hvc_rtas_console_init); |
| 112 | |