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 .notifier_add
= notifier_add_irq
,
56 .notifier_del
= notifier_del_irq
,
57 .notifier_hangup
= notifier_hangup_irq
,
60 static int hvc_opal_hvsi_get_chars(uint32_t vtermno
, char *buf
, int count
)
62 struct hvc_opal_priv
*pv
= hvc_opal_privs
[vtermno
];
67 return hvsilib_get_chars(&pv
->hvsi
, buf
, count
);
70 static int hvc_opal_hvsi_put_chars(uint32_t vtermno
, const char *buf
, int count
)
72 struct hvc_opal_priv
*pv
= hvc_opal_privs
[vtermno
];
77 return hvsilib_put_chars(&pv
->hvsi
, buf
, count
);
80 static int hvc_opal_hvsi_open(struct hvc_struct
*hp
, int data
)
82 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
85 pr_devel("HVSI@%x: do open !\n", hp
->vtermno
);
87 rc
= notifier_add_irq(hp
, data
);
91 return hvsilib_open(&pv
->hvsi
, hp
);
94 static void hvc_opal_hvsi_close(struct hvc_struct
*hp
, int data
)
96 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
98 pr_devel("HVSI@%x: do close !\n", hp
->vtermno
);
100 hvsilib_close(&pv
->hvsi
, hp
);
102 notifier_del_irq(hp
, data
);
105 void hvc_opal_hvsi_hangup(struct hvc_struct
*hp
, int data
)
107 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
109 pr_devel("HVSI@%x: do hangup !\n", hp
->vtermno
);
111 hvsilib_close(&pv
->hvsi
, hp
);
113 notifier_hangup_irq(hp
, data
);
116 static int hvc_opal_hvsi_tiocmget(struct hvc_struct
*hp
)
118 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
122 return pv
->hvsi
.mctrl
;
125 static int hvc_opal_hvsi_tiocmset(struct hvc_struct
*hp
, unsigned int set
,
128 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
130 pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
131 hp
->vtermno
, set
, clear
);
134 hvsilib_write_mctrl(&pv
->hvsi
, 1);
135 else if (clear
& TIOCM_DTR
)
136 hvsilib_write_mctrl(&pv
->hvsi
, 0);
141 static const struct hv_ops hvc_opal_hvsi_ops
= {
142 .get_chars
= hvc_opal_hvsi_get_chars
,
143 .put_chars
= hvc_opal_hvsi_put_chars
,
144 .notifier_add
= hvc_opal_hvsi_open
,
145 .notifier_del
= hvc_opal_hvsi_close
,
146 .notifier_hangup
= hvc_opal_hvsi_hangup
,
147 .tiocmget
= hvc_opal_hvsi_tiocmget
,
148 .tiocmset
= hvc_opal_hvsi_tiocmset
,
151 static int hvc_opal_probe(struct platform_device
*dev
)
153 const struct hv_ops
*ops
;
154 struct hvc_struct
*hp
;
155 struct hvc_opal_priv
*pv
;
157 unsigned int termno
, irq
, boot
= 0;
160 if (of_device_is_compatible(dev
->dev
.of_node
, "ibm,opal-console-raw")) {
161 proto
= HV_PROTOCOL_RAW
;
162 ops
= &hvc_opal_raw_ops
;
163 } else if (of_device_is_compatible(dev
->dev
.of_node
,
164 "ibm,opal-console-hvsi")) {
165 proto
= HV_PROTOCOL_HVSI
;
166 ops
= &hvc_opal_hvsi_ops
;
168 pr_err("hvc_opal: Unknown protocol for %pOF\n",
173 reg
= of_get_property(dev
->dev
.of_node
, "reg", NULL
);
174 termno
= reg
? be32_to_cpup(reg
) : 0;
176 /* Is it our boot one ? */
177 if (hvc_opal_privs
[termno
] == &hvc_opal_boot_priv
) {
178 pv
= hvc_opal_privs
[termno
];
180 } else if (hvc_opal_privs
[termno
] == NULL
) {
181 pv
= kzalloc(sizeof(struct hvc_opal_priv
), GFP_KERNEL
);
185 hvc_opal_privs
[termno
] = pv
;
186 if (proto
== HV_PROTOCOL_HVSI
)
187 hvsilib_init(&pv
->hvsi
, opal_get_chars
, opal_put_chars
,
190 /* Instanciate now to establish a mapping index==vtermno */
191 hvc_instantiate(termno
, termno
, ops
);
193 pr_err("hvc_opal: Device %pOF has duplicate terminal number #%d\n",
194 dev
->dev
.of_node
, termno
);
198 pr_info("hvc%d: %s protocol on %pOF%s\n", termno
,
199 proto
== HV_PROTOCOL_RAW
? "raw" : "hvsi",
201 boot
? " (boot console)" : "");
203 irq
= irq_of_parse_and_map(dev
->dev
.of_node
, 0);
205 pr_info("hvc%d: No interrupts property, using OPAL event\n",
207 irq
= opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT
));
211 pr_err("hvc_opal: Unable to map interrupt for device %pOF\n",
216 hp
= hvc_alloc(termno
, irq
, ops
, MAX_VIO_PUT_CHARS
);
220 /* hvc consoles on powernv may need to share a single irq */
221 hp
->flags
= IRQF_SHARED
;
222 dev_set_drvdata(&dev
->dev
, hp
);
227 static int hvc_opal_remove(struct platform_device
*dev
)
229 struct hvc_struct
*hp
= dev_get_drvdata(&dev
->dev
);
232 termno
= hp
->vtermno
;
235 if (hvc_opal_privs
[termno
] != &hvc_opal_boot_priv
)
236 kfree(hvc_opal_privs
[termno
]);
237 hvc_opal_privs
[termno
] = NULL
;
242 static struct platform_driver hvc_opal_driver
= {
243 .probe
= hvc_opal_probe
,
244 .remove
= hvc_opal_remove
,
246 .name
= hvc_opal_name
,
247 .of_match_table
= hvc_opal_match
,
251 static int __init
hvc_opal_init(void)
253 if (!firmware_has_feature(FW_FEATURE_OPAL
))
256 /* Register as a vio device to receive callbacks */
257 return platform_driver_register(&hvc_opal_driver
);
259 device_initcall(hvc_opal_init
);
261 static void udbg_opal_putc(char c
)
263 unsigned int termno
= hvc_opal_boot_termno
;
267 udbg_opal_putc('\r');
270 switch(hvc_opal_boot_priv
.proto
) {
271 case HV_PROTOCOL_RAW
:
272 count
= opal_put_chars(termno
, &c
, 1);
274 case HV_PROTOCOL_HVSI
:
275 count
= hvc_opal_hvsi_put_chars(termno
, &c
, 1);
278 } while(count
== 0 || count
== -EAGAIN
);
281 static int udbg_opal_getc_poll(void)
283 unsigned int termno
= hvc_opal_boot_termno
;
287 switch(hvc_opal_boot_priv
.proto
) {
288 case HV_PROTOCOL_RAW
:
289 rc
= opal_get_chars(termno
, &c
, 1);
291 case HV_PROTOCOL_HVSI
:
292 rc
= hvc_opal_hvsi_get_chars(termno
, &c
, 1);
300 static int udbg_opal_getc(void)
304 ch
= udbg_opal_getc_poll();
306 /* This shouldn't be needed...but... */
307 volatile unsigned long delay
;
308 for (delay
=0; delay
< 2000000; delay
++)
316 static void udbg_init_opal_common(void)
318 udbg_putc
= udbg_opal_putc
;
319 udbg_getc
= udbg_opal_getc
;
320 udbg_getc_poll
= udbg_opal_getc_poll
;
321 tb_ticks_per_usec
= 0x200; /* Make udelay not suck */
324 void __init
hvc_opal_init_early(void)
326 struct device_node
*stdout_node
= of_node_get(of_stdout
);
327 const __be32
*termno
;
328 const struct hv_ops
*ops
;
331 /* If the console wasn't in /chosen, try /ibm,opal */
333 struct device_node
*opal
, *np
;
335 /* Current OPAL takeover doesn't provide the stdout
336 * path, so we hard wire it
338 opal
= of_find_node_by_path("/ibm,opal/consoles");
340 pr_devel("hvc_opal: Found consoles in new location\n");
342 opal
= of_find_node_by_path("/ibm,opal");
344 pr_devel("hvc_opal: "
345 "Found consoles in old location\n");
349 for_each_child_of_node(opal
, np
) {
350 if (!strcmp(np
->name
, "serial")) {
359 termno
= of_get_property(stdout_node
, "reg", NULL
);
360 index
= termno
? be32_to_cpup(termno
) : 0;
361 if (index
>= MAX_NR_HVC_CONSOLES
)
363 hvc_opal_privs
[index
] = &hvc_opal_boot_priv
;
365 /* Check the protocol */
366 if (of_device_is_compatible(stdout_node
, "ibm,opal-console-raw")) {
367 hvc_opal_boot_priv
.proto
= HV_PROTOCOL_RAW
;
368 ops
= &hvc_opal_raw_ops
;
369 pr_devel("hvc_opal: Found RAW console\n");
371 else if (of_device_is_compatible(stdout_node
,"ibm,opal-console-hvsi")) {
372 hvc_opal_boot_priv
.proto
= HV_PROTOCOL_HVSI
;
373 ops
= &hvc_opal_hvsi_ops
;
374 hvsilib_init(&hvc_opal_boot_priv
.hvsi
, opal_get_chars
,
375 opal_put_chars
, index
, 1);
376 /* HVSI, perform the handshake now */
377 hvsilib_establish(&hvc_opal_boot_priv
.hvsi
);
378 pr_devel("hvc_opal: Found HVSI console\n");
381 hvc_opal_boot_termno
= index
;
382 udbg_init_opal_common();
383 add_preferred_console("hvc", index
, NULL
);
384 hvc_instantiate(index
, index
, ops
);
386 of_node_put(stdout_node
);
389 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
390 void __init
udbg_init_debug_opal_raw(void)
392 u32 index
= CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO
;
393 hvc_opal_privs
[index
] = &hvc_opal_boot_priv
;
394 hvc_opal_boot_priv
.proto
= HV_PROTOCOL_RAW
;
395 hvc_opal_boot_termno
= index
;
396 udbg_init_opal_common();
398 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
400 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
401 void __init
udbg_init_debug_opal_hvsi(void)
403 u32 index
= CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO
;
404 hvc_opal_privs
[index
] = &hvc_opal_boot_priv
;
405 hvc_opal_boot_termno
= index
;
406 udbg_init_opal_common();
407 hvsilib_init(&hvc_opal_boot_priv
.hvsi
, opal_get_chars
, opal_put_chars
,
409 hvsilib_establish(&hvc_opal_boot_priv
.hvsi
);
411 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */