1 // SPDX-License-Identifier: GPL-2.0+
3 * opal driver interface to hvc_console.c
5 * Copyright 2011 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
10 #include <linux/types.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/console.h>
16 #include <linux/of_platform.h>
17 #include <linux/export.h>
18 #include <linux/interrupt.h>
20 #include <asm/hvconsole.h>
22 #include <asm/firmware.h>
27 #include "hvc_console.h"
29 static const char hvc_opal_name[] = "hvc_opal";
31 static const struct of_device_id hvc_opal_match[] = {
32 { .name = "serial", .compatible = "ibm,opal-console-raw" },
33 { .name = "serial", .compatible = "ibm,opal-console-hvsi" },
37 typedef enum hv_protocol {
42 struct hvc_opal_priv {
43 hv_protocol_t proto; /* Raw data or HVSI packets */
44 struct hvsi_priv hvsi; /* HVSI specific data */
46 static struct hvc_opal_priv *hvc_opal_privs[MAX_NR_HVC_CONSOLES];
48 /* For early boot console */
49 static struct hvc_opal_priv hvc_opal_boot_priv;
50 static u32 hvc_opal_boot_termno;
52 static const struct hv_ops hvc_opal_raw_ops = {
53 .get_chars = opal_get_chars,
54 .put_chars = opal_put_chars,
55 .flush = opal_flush_chars,
56 .notifier_add = notifier_add_irq,
57 .notifier_del = notifier_del_irq,
58 .notifier_hangup = notifier_hangup_irq,
61 static int hvc_opal_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
63 struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
68 return hvsilib_get_chars(&pv->hvsi, buf, count);
71 static int hvc_opal_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
73 struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
78 return hvsilib_put_chars(&pv->hvsi, buf, count);
81 static int hvc_opal_hvsi_open(struct hvc_struct *hp, int data)
83 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
86 pr_devel("HVSI@%x: do open !\n", hp->vtermno);
88 rc = notifier_add_irq(hp, data);
92 return hvsilib_open(&pv->hvsi, hp);
95 static void hvc_opal_hvsi_close(struct hvc_struct *hp, int data)
97 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
99 pr_devel("HVSI@%x: do close !\n", hp->vtermno);
101 hvsilib_close(&pv->hvsi, hp);
103 notifier_del_irq(hp, data);
106 void hvc_opal_hvsi_hangup(struct hvc_struct *hp, int data)
108 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
110 pr_devel("HVSI@%x: do hangup !\n", hp->vtermno);
112 hvsilib_close(&pv->hvsi, hp);
114 notifier_hangup_irq(hp, data);
117 static int hvc_opal_hvsi_tiocmget(struct hvc_struct *hp)
119 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
123 return pv->hvsi.mctrl;
126 static int hvc_opal_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
129 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
131 pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
132 hp->vtermno, set, clear);
135 hvsilib_write_mctrl(&pv->hvsi, 1);
136 else if (clear & TIOCM_DTR)
137 hvsilib_write_mctrl(&pv->hvsi, 0);
142 static const struct hv_ops hvc_opal_hvsi_ops = {
143 .get_chars = hvc_opal_hvsi_get_chars,
144 .put_chars = hvc_opal_hvsi_put_chars,
145 .flush = opal_flush_chars,
146 .notifier_add = hvc_opal_hvsi_open,
147 .notifier_del = hvc_opal_hvsi_close,
148 .notifier_hangup = hvc_opal_hvsi_hangup,
149 .tiocmget = hvc_opal_hvsi_tiocmget,
150 .tiocmset = hvc_opal_hvsi_tiocmset,
153 static int hvc_opal_probe(struct platform_device *dev)
155 const struct hv_ops *ops;
156 struct hvc_struct *hp;
157 struct hvc_opal_priv *pv;
159 unsigned int termno, irq, boot = 0;
162 if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) {
163 proto = HV_PROTOCOL_RAW;
164 ops = &hvc_opal_raw_ops;
165 } else if (of_device_is_compatible(dev->dev.of_node,
166 "ibm,opal-console-hvsi")) {
167 proto = HV_PROTOCOL_HVSI;
168 ops = &hvc_opal_hvsi_ops;
170 pr_err("hvc_opal: Unknown protocol for %pOF\n",
175 reg = of_get_property(dev->dev.of_node, "reg", NULL);
176 termno = reg ? be32_to_cpup(reg) : 0;
178 /* Is it our boot one ? */
179 if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
180 pv = hvc_opal_privs[termno];
182 } else if (hvc_opal_privs[termno] == NULL) {
183 pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL);
187 hvc_opal_privs[termno] = pv;
188 if (proto == HV_PROTOCOL_HVSI) {
190 * We want put_chars to be atomic to avoid mangling of
193 hvsilib_init(&pv->hvsi,
194 opal_get_chars, opal_put_chars_atomic,
198 /* Instanciate now to establish a mapping index==vtermno */
199 hvc_instantiate(termno, termno, ops);
201 pr_err("hvc_opal: Device %pOF has duplicate terminal number #%d\n",
202 dev->dev.of_node, termno);
206 pr_info("hvc%d: %s protocol on %pOF%s\n", termno,
207 proto == HV_PROTOCOL_RAW ? "raw" : "hvsi",
209 boot ? " (boot console)" : "");
211 irq = irq_of_parse_and_map(dev->dev.of_node, 0);
213 pr_info("hvc%d: No interrupts property, using OPAL event\n",
215 irq = opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT));
219 pr_err("hvc_opal: Unable to map interrupt for device %pOF\n",
224 hp = hvc_alloc(termno, irq, ops, MAX_VIO_PUT_CHARS);
228 /* hvc consoles on powernv may need to share a single irq */
229 hp->flags = IRQF_SHARED;
230 dev_set_drvdata(&dev->dev, hp);
235 static int hvc_opal_remove(struct platform_device *dev)
237 struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
240 termno = hp->vtermno;
243 if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
244 kfree(hvc_opal_privs[termno]);
245 hvc_opal_privs[termno] = NULL;
250 static struct platform_driver hvc_opal_driver = {
251 .probe = hvc_opal_probe,
252 .remove = hvc_opal_remove,
254 .name = hvc_opal_name,
255 .of_match_table = hvc_opal_match,
259 static int __init hvc_opal_init(void)
261 if (!firmware_has_feature(FW_FEATURE_OPAL))
264 /* Register as a vio device to receive callbacks */
265 return platform_driver_register(&hvc_opal_driver);
267 device_initcall(hvc_opal_init);
269 static void udbg_opal_putc(char c)
271 unsigned int termno = hvc_opal_boot_termno;
275 udbg_opal_putc('\r');
278 switch(hvc_opal_boot_priv.proto) {
279 case HV_PROTOCOL_RAW:
280 count = opal_put_chars(termno, &c, 1);
282 case HV_PROTOCOL_HVSI:
283 count = hvc_opal_hvsi_put_chars(termno, &c, 1);
287 /* This is needed for the cosole to flush
288 * when there aren't any interrupts.
290 opal_flush_console(termno);
291 } while(count == 0 || count == -EAGAIN);
294 static int udbg_opal_getc_poll(void)
296 unsigned int termno = hvc_opal_boot_termno;
300 switch(hvc_opal_boot_priv.proto) {
301 case HV_PROTOCOL_RAW:
302 rc = opal_get_chars(termno, &c, 1);
304 case HV_PROTOCOL_HVSI:
305 rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
313 static int udbg_opal_getc(void)
317 ch = udbg_opal_getc_poll();
323 static void udbg_init_opal_common(void)
325 udbg_putc = udbg_opal_putc;
326 udbg_getc = udbg_opal_getc;
327 udbg_getc_poll = udbg_opal_getc_poll;
330 void __init hvc_opal_init_early(void)
332 struct device_node *stdout_node = of_node_get(of_stdout);
333 const __be32 *termno;
334 const struct hv_ops *ops;
337 /* If the console wasn't in /chosen, try /ibm,opal */
339 struct device_node *opal, *np;
341 /* Current OPAL takeover doesn't provide the stdout
342 * path, so we hard wire it
344 opal = of_find_node_by_path("/ibm,opal/consoles");
346 pr_devel("hvc_opal: Found consoles in new location\n");
348 opal = of_find_node_by_path("/ibm,opal");
350 pr_devel("hvc_opal: "
351 "Found consoles in old location\n");
355 for_each_child_of_node(opal, np) {
356 if (of_node_name_eq(np, "serial")) {
365 termno = of_get_property(stdout_node, "reg", NULL);
366 index = termno ? be32_to_cpup(termno) : 0;
367 if (index >= MAX_NR_HVC_CONSOLES)
369 hvc_opal_privs[index] = &hvc_opal_boot_priv;
371 /* Check the protocol */
372 if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
373 hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
374 ops = &hvc_opal_raw_ops;
375 pr_devel("hvc_opal: Found RAW console\n");
377 else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
378 hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
379 ops = &hvc_opal_hvsi_ops;
380 hvsilib_init(&hvc_opal_boot_priv.hvsi,
381 opal_get_chars, opal_put_chars_atomic,
383 /* HVSI, perform the handshake now */
384 hvsilib_establish(&hvc_opal_boot_priv.hvsi);
385 pr_devel("hvc_opal: Found HVSI console\n");
388 hvc_opal_boot_termno = index;
389 udbg_init_opal_common();
390 add_preferred_console("hvc", index, NULL);
391 hvc_instantiate(index, index, ops);
393 of_node_put(stdout_node);
396 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
397 void __init udbg_init_debug_opal_raw(void)
399 u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
400 hvc_opal_privs[index] = &hvc_opal_boot_priv;
401 hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
402 hvc_opal_boot_termno = index;
403 udbg_init_opal_common();
405 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
407 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
408 void __init udbg_init_debug_opal_hvsi(void)
410 u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
411 hvc_opal_privs[index] = &hvc_opal_boot_priv;
412 hvc_opal_boot_termno = index;
413 udbg_init_opal_common();
414 hvsilib_init(&hvc_opal_boot_priv.hvsi,
415 opal_get_chars, opal_put_chars_atomic,
417 hvsilib_establish(&hvc_opal_boot_priv.hvsi);
419 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */