1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * udbg interface to hvc_console.c 4 * 5 * (C) Copyright David Gibson, IBM Corporation 2008. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 #include <linux/console.h> 23 #include <linux/delay.h> 24 #include <linux/err.h> 25 #include <linux/init.h> 26 #include <linux/moduleparam.h> 27 #include <linux/types.h> 28 #include <linux/irq.h> 29 30 #include <asm/udbg.h> 31 32 #include "hvc_console.h" 33 34 struct hvc_struct *hvc_udbg_dev; 35 36 static int hvc_udbg_put(uint32_t vtermno, const char *buf, int count) 37 { 38 int i; 39 40 for (i = 0; i < count && udbg_putc; i++) 41 udbg_putc(buf[i]); 42 43 return i; 44 } 45 46 static int hvc_udbg_get(uint32_t vtermno, char *buf, int count) 47 { 48 int i, c; 49 50 if (!udbg_getc_poll) 51 return 0; 52 53 for (i = 0; i < count; i++) { 54 if ((c = udbg_getc_poll()) == -1) 55 break; 56 buf[i] = c; 57 } 58 59 return i; 60 } 61 62 static const struct hv_ops hvc_udbg_ops = { 63 .get_chars = hvc_udbg_get, 64 .put_chars = hvc_udbg_put, 65 }; 66 67 static int __init hvc_udbg_init(void) 68 { 69 struct hvc_struct *hp; 70 71 if (!udbg_putc) 72 return -ENODEV; 73 74 BUG_ON(hvc_udbg_dev); 75 76 hp = hvc_alloc(0, 0, &hvc_udbg_ops, 16); 77 if (IS_ERR(hp)) 78 return PTR_ERR(hp); 79 80 hvc_udbg_dev = hp; 81 82 return 0; 83 } 84 device_initcall(hvc_udbg_init); 85 86 static int __init hvc_udbg_console_init(void) 87 { 88 if (!udbg_putc) 89 return -ENODEV; 90 91 hvc_instantiate(0, 0, &hvc_udbg_ops); 92 add_preferred_console("hvc", 0, NULL); 93 94 return 0; 95 } 96 console_initcall(hvc_udbg_console_init); 97