xref: /openbmc/linux/drivers/tty/hvc/hvc_rtas.c (revision cc72a1ee)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2728674a7SGreg Kroah-Hartman /*
3728674a7SGreg Kroah-Hartman  * IBM RTAS driver interface to hvc_console.c
4728674a7SGreg Kroah-Hartman  *
5728674a7SGreg Kroah-Hartman  * (C) Copyright IBM Corporation 2001-2005
6728674a7SGreg Kroah-Hartman  * (C) Copyright Red Hat, Inc. 2005
7728674a7SGreg Kroah-Hartman  *
8728674a7SGreg Kroah-Hartman  * Author(s): Maximino Augilar <IBM STI Design Center>
9728674a7SGreg Kroah-Hartman  *	    : Ryan S. Arnold <rsa@us.ibm.com>
10728674a7SGreg Kroah-Hartman  *	    : Utz Bacher <utz.bacher@de.ibm.com>
11728674a7SGreg Kroah-Hartman  *	    : David Woodhouse <dwmw2@infradead.org>
12728674a7SGreg Kroah-Hartman  *
13728674a7SGreg Kroah-Hartman  *    inspired by drivers/char/hvc_console.c
14728674a7SGreg Kroah-Hartman  *    written by Anton Blanchard and Paul Mackerras
15728674a7SGreg Kroah-Hartman  */
16728674a7SGreg Kroah-Hartman 
17728674a7SGreg Kroah-Hartman #include <linux/console.h>
18728674a7SGreg Kroah-Hartman #include <linux/delay.h>
19728674a7SGreg Kroah-Hartman #include <linux/err.h>
20728674a7SGreg Kroah-Hartman #include <linux/init.h>
21728674a7SGreg Kroah-Hartman #include <linux/moduleparam.h>
22728674a7SGreg Kroah-Hartman #include <linux/types.h>
23728674a7SGreg Kroah-Hartman 
24728674a7SGreg Kroah-Hartman #include <asm/irq.h>
25728674a7SGreg Kroah-Hartman #include <asm/rtas.h>
26728674a7SGreg Kroah-Hartman #include "hvc_console.h"
27728674a7SGreg Kroah-Hartman 
28728674a7SGreg Kroah-Hartman #define hvc_rtas_cookie 0x67781e15
29*cc72a1eeSruanjinjie static struct hvc_struct *hvc_rtas_dev;
30728674a7SGreg Kroah-Hartman 
31728674a7SGreg Kroah-Hartman static int rtascons_put_char_token = RTAS_UNKNOWN_SERVICE;
32728674a7SGreg Kroah-Hartman static int rtascons_get_char_token = RTAS_UNKNOWN_SERVICE;
33728674a7SGreg Kroah-Hartman 
hvc_rtas_write_console(uint32_t vtermno,const char * buf,int count)34728674a7SGreg Kroah-Hartman static inline int hvc_rtas_write_console(uint32_t vtermno, const char *buf,
35728674a7SGreg Kroah-Hartman 		int count)
36728674a7SGreg Kroah-Hartman {
37728674a7SGreg Kroah-Hartman 	int i;
38728674a7SGreg Kroah-Hartman 
39728674a7SGreg Kroah-Hartman 	for (i = 0; i < count; i++) {
40728674a7SGreg Kroah-Hartman 		if (rtas_call(rtascons_put_char_token, 1, 1, NULL, buf[i]))
41728674a7SGreg Kroah-Hartman 			break;
42728674a7SGreg Kroah-Hartman 	}
43728674a7SGreg Kroah-Hartman 
44728674a7SGreg Kroah-Hartman 	return i;
45728674a7SGreg Kroah-Hartman }
46728674a7SGreg Kroah-Hartman 
hvc_rtas_read_console(uint32_t vtermno,char * buf,int count)47728674a7SGreg Kroah-Hartman static int hvc_rtas_read_console(uint32_t vtermno, char *buf, int count)
48728674a7SGreg Kroah-Hartman {
49728674a7SGreg Kroah-Hartman 	int i, c;
50728674a7SGreg Kroah-Hartman 
51728674a7SGreg Kroah-Hartman 	for (i = 0; i < count; i++) {
52728674a7SGreg Kroah-Hartman 		if (rtas_call(rtascons_get_char_token, 0, 2, &c))
53728674a7SGreg Kroah-Hartman 			break;
54728674a7SGreg Kroah-Hartman 
55728674a7SGreg Kroah-Hartman 		buf[i] = c;
56728674a7SGreg Kroah-Hartman 	}
57728674a7SGreg Kroah-Hartman 
58728674a7SGreg Kroah-Hartman 	return i;
59728674a7SGreg Kroah-Hartman }
60728674a7SGreg Kroah-Hartman 
61728674a7SGreg Kroah-Hartman static const struct hv_ops hvc_rtas_get_put_ops = {
62728674a7SGreg Kroah-Hartman 	.get_chars = hvc_rtas_read_console,
63728674a7SGreg Kroah-Hartman 	.put_chars = hvc_rtas_write_console,
64728674a7SGreg Kroah-Hartman };
65728674a7SGreg Kroah-Hartman 
hvc_rtas_init(void)66728674a7SGreg Kroah-Hartman static int __init hvc_rtas_init(void)
67728674a7SGreg Kroah-Hartman {
68728674a7SGreg Kroah-Hartman 	struct hvc_struct *hp;
69728674a7SGreg Kroah-Hartman 
70728674a7SGreg Kroah-Hartman 	if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
71728674a7SGreg Kroah-Hartman 		rtascons_put_char_token = rtas_token("put-term-char");
72728674a7SGreg Kroah-Hartman 	if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
73728674a7SGreg Kroah-Hartman 		return -EIO;
74728674a7SGreg Kroah-Hartman 
75728674a7SGreg Kroah-Hartman 	if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
76728674a7SGreg Kroah-Hartman 		rtascons_get_char_token = rtas_token("get-term-char");
77728674a7SGreg Kroah-Hartman 	if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
78728674a7SGreg Kroah-Hartman 		return -EIO;
79728674a7SGreg Kroah-Hartman 
80728674a7SGreg Kroah-Hartman 	BUG_ON(hvc_rtas_dev);
81728674a7SGreg Kroah-Hartman 
82728674a7SGreg Kroah-Hartman 	/* Allocate an hvc_struct for the console device we instantiated
83728674a7SGreg Kroah-Hartman 	 * earlier.  Save off hp so that we can return it on exit */
84d4e33facSAlan Cox 	hp = hvc_alloc(hvc_rtas_cookie, 0, &hvc_rtas_get_put_ops, 16);
85728674a7SGreg Kroah-Hartman 	if (IS_ERR(hp))
86728674a7SGreg Kroah-Hartman 		return PTR_ERR(hp);
87728674a7SGreg Kroah-Hartman 
88728674a7SGreg Kroah-Hartman 	hvc_rtas_dev = hp;
89728674a7SGreg Kroah-Hartman 
90728674a7SGreg Kroah-Hartman 	return 0;
91728674a7SGreg Kroah-Hartman }
924fedd0bfSPaul Gortmaker device_initcall(hvc_rtas_init);
93728674a7SGreg Kroah-Hartman 
94728674a7SGreg Kroah-Hartman /* This will happen prior to module init.  There is no tty at this time? */
hvc_rtas_console_init(void)95728674a7SGreg Kroah-Hartman static int __init hvc_rtas_console_init(void)
96728674a7SGreg Kroah-Hartman {
97728674a7SGreg Kroah-Hartman 	rtascons_put_char_token = rtas_token("put-term-char");
98728674a7SGreg Kroah-Hartman 	if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
99728674a7SGreg Kroah-Hartman 		return -EIO;
100728674a7SGreg Kroah-Hartman 
101728674a7SGreg Kroah-Hartman 	rtascons_get_char_token = rtas_token("get-term-char");
102728674a7SGreg Kroah-Hartman 	if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
103728674a7SGreg Kroah-Hartman 		return -EIO;
104728674a7SGreg Kroah-Hartman 
105728674a7SGreg Kroah-Hartman 	hvc_instantiate(hvc_rtas_cookie, 0, &hvc_rtas_get_put_ops);
106728674a7SGreg Kroah-Hartman 	add_preferred_console("hvc", 0, NULL);
107728674a7SGreg Kroah-Hartman 
108728674a7SGreg Kroah-Hartman 	return 0;
109728674a7SGreg Kroah-Hartman }
110728674a7SGreg Kroah-Hartman console_initcall(hvc_rtas_console_init);
111