1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2728674a7SGreg Kroah-Hartman /*
3728674a7SGreg Kroah-Hartman * udbg interface to hvc_console.c
4728674a7SGreg Kroah-Hartman *
5728674a7SGreg Kroah-Hartman * (C) Copyright David Gibson, IBM Corporation 2008.
6728674a7SGreg Kroah-Hartman */
7728674a7SGreg Kroah-Hartman
8728674a7SGreg Kroah-Hartman #include <linux/console.h>
9728674a7SGreg Kroah-Hartman #include <linux/delay.h>
10728674a7SGreg Kroah-Hartman #include <linux/err.h>
11728674a7SGreg Kroah-Hartman #include <linux/init.h>
12728674a7SGreg Kroah-Hartman #include <linux/moduleparam.h>
13728674a7SGreg Kroah-Hartman #include <linux/types.h>
14728674a7SGreg Kroah-Hartman #include <linux/irq.h>
15728674a7SGreg Kroah-Hartman
16728674a7SGreg Kroah-Hartman #include <asm/udbg.h>
17728674a7SGreg Kroah-Hartman
18728674a7SGreg Kroah-Hartman #include "hvc_console.h"
19728674a7SGreg Kroah-Hartman
20*63bbdb4eSYu Kuai static struct hvc_struct *hvc_udbg_dev;
21728674a7SGreg Kroah-Hartman
hvc_udbg_put(uint32_t vtermno,const char * buf,int count)22728674a7SGreg Kroah-Hartman static int hvc_udbg_put(uint32_t vtermno, const char *buf, int count)
23728674a7SGreg Kroah-Hartman {
24728674a7SGreg Kroah-Hartman int i;
25728674a7SGreg Kroah-Hartman
267d3d897aSBenjamin Herrenschmidt for (i = 0; i < count && udbg_putc; i++)
27728674a7SGreg Kroah-Hartman udbg_putc(buf[i]);
28728674a7SGreg Kroah-Hartman
29728674a7SGreg Kroah-Hartman return i;
30728674a7SGreg Kroah-Hartman }
31728674a7SGreg Kroah-Hartman
hvc_udbg_get(uint32_t vtermno,char * buf,int count)32728674a7SGreg Kroah-Hartman static int hvc_udbg_get(uint32_t vtermno, char *buf, int count)
33728674a7SGreg Kroah-Hartman {
34728674a7SGreg Kroah-Hartman int i, c;
35728674a7SGreg Kroah-Hartman
36728674a7SGreg Kroah-Hartman if (!udbg_getc_poll)
37728674a7SGreg Kroah-Hartman return 0;
38728674a7SGreg Kroah-Hartman
39728674a7SGreg Kroah-Hartman for (i = 0; i < count; i++) {
40728674a7SGreg Kroah-Hartman if ((c = udbg_getc_poll()) == -1)
41728674a7SGreg Kroah-Hartman break;
42728674a7SGreg Kroah-Hartman buf[i] = c;
43728674a7SGreg Kroah-Hartman }
44728674a7SGreg Kroah-Hartman
45728674a7SGreg Kroah-Hartman return i;
46728674a7SGreg Kroah-Hartman }
47728674a7SGreg Kroah-Hartman
48728674a7SGreg Kroah-Hartman static const struct hv_ops hvc_udbg_ops = {
49728674a7SGreg Kroah-Hartman .get_chars = hvc_udbg_get,
50728674a7SGreg Kroah-Hartman .put_chars = hvc_udbg_put,
51728674a7SGreg Kroah-Hartman };
52728674a7SGreg Kroah-Hartman
hvc_udbg_init(void)53728674a7SGreg Kroah-Hartman static int __init hvc_udbg_init(void)
54728674a7SGreg Kroah-Hartman {
55728674a7SGreg Kroah-Hartman struct hvc_struct *hp;
56728674a7SGreg Kroah-Hartman
577d3d897aSBenjamin Herrenschmidt if (!udbg_putc)
587d3d897aSBenjamin Herrenschmidt return -ENODEV;
597d3d897aSBenjamin Herrenschmidt
60728674a7SGreg Kroah-Hartman BUG_ON(hvc_udbg_dev);
61728674a7SGreg Kroah-Hartman
62d4e33facSAlan Cox hp = hvc_alloc(0, 0, &hvc_udbg_ops, 16);
63728674a7SGreg Kroah-Hartman if (IS_ERR(hp))
64728674a7SGreg Kroah-Hartman return PTR_ERR(hp);
65728674a7SGreg Kroah-Hartman
66728674a7SGreg Kroah-Hartman hvc_udbg_dev = hp;
67728674a7SGreg Kroah-Hartman
68728674a7SGreg Kroah-Hartman return 0;
69728674a7SGreg Kroah-Hartman }
704fedd0bfSPaul Gortmaker device_initcall(hvc_udbg_init);
71728674a7SGreg Kroah-Hartman
hvc_udbg_console_init(void)72728674a7SGreg Kroah-Hartman static int __init hvc_udbg_console_init(void)
73728674a7SGreg Kroah-Hartman {
747d3d897aSBenjamin Herrenschmidt if (!udbg_putc)
757d3d897aSBenjamin Herrenschmidt return -ENODEV;
767d3d897aSBenjamin Herrenschmidt
77728674a7SGreg Kroah-Hartman hvc_instantiate(0, 0, &hvc_udbg_ops);
78728674a7SGreg Kroah-Hartman add_preferred_console("hvc", 0, NULL);
79728674a7SGreg Kroah-Hartman
80728674a7SGreg Kroah-Hartman return 0;
81728674a7SGreg Kroah-Hartman }
82728674a7SGreg Kroah-Hartman console_initcall(hvc_udbg_console_init);
83