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_irq.h>
17 #include <linux/platform_device.h>
18 #include <linux/export.h>
19 #include <linux/interrupt.h>
21 #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 ssize_t
hvc_opal_hvsi_get_chars(uint32_t vtermno
, u8
*buf
, size_t count
)
63 struct hvc_opal_priv
*pv
= hvc_opal_privs
[vtermno
];
68 return hvsilib_get_chars(&pv
->hvsi
, buf
, count
);
71 static ssize_t
hvc_opal_hvsi_put_chars(uint32_t vtermno
, const u8
*buf
,
74 struct hvc_opal_priv
*pv
= hvc_opal_privs
[vtermno
];
79 return hvsilib_put_chars(&pv
->hvsi
, buf
, count
);
82 static int hvc_opal_hvsi_open(struct hvc_struct
*hp
, int data
)
84 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
87 pr_devel("HVSI@%x: do open !\n", hp
->vtermno
);
89 rc
= notifier_add_irq(hp
, data
);
93 return hvsilib_open(&pv
->hvsi
, hp
);
96 static void hvc_opal_hvsi_close(struct hvc_struct
*hp
, int data
)
98 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
100 pr_devel("HVSI@%x: do close !\n", hp
->vtermno
);
102 hvsilib_close(&pv
->hvsi
, hp
);
104 notifier_del_irq(hp
, data
);
107 static void hvc_opal_hvsi_hangup(struct hvc_struct
*hp
, int data
)
109 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
111 pr_devel("HVSI@%x: do hangup !\n", hp
->vtermno
);
113 hvsilib_close(&pv
->hvsi
, hp
);
115 notifier_hangup_irq(hp
, data
);
118 static int hvc_opal_hvsi_tiocmget(struct hvc_struct
*hp
)
120 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
124 return pv
->hvsi
.mctrl
;
127 static int hvc_opal_hvsi_tiocmset(struct hvc_struct
*hp
, unsigned int set
,
130 struct hvc_opal_priv
*pv
= hvc_opal_privs
[hp
->vtermno
];
132 pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
133 hp
->vtermno
, set
, clear
);
136 hvsilib_write_mctrl(&pv
->hvsi
, 1);
137 else if (clear
& TIOCM_DTR
)
138 hvsilib_write_mctrl(&pv
->hvsi
, 0);
143 static const struct hv_ops hvc_opal_hvsi_ops
= {
144 .get_chars
= hvc_opal_hvsi_get_chars
,
145 .put_chars
= hvc_opal_hvsi_put_chars
,
146 .flush
= opal_flush_chars
,
147 .notifier_add
= hvc_opal_hvsi_open
,
148 .notifier_del
= hvc_opal_hvsi_close
,
149 .notifier_hangup
= hvc_opal_hvsi_hangup
,
150 .tiocmget
= hvc_opal_hvsi_tiocmget
,
151 .tiocmset
= hvc_opal_hvsi_tiocmset
,
154 static int hvc_opal_probe(struct platform_device
*dev
)
156 const struct hv_ops
*ops
;
157 struct hvc_struct
*hp
;
158 struct hvc_opal_priv
*pv
;
160 unsigned int termno
, irq
, boot
= 0;
163 if (of_device_is_compatible(dev
->dev
.of_node
, "ibm,opal-console-raw")) {
164 proto
= HV_PROTOCOL_RAW
;
165 ops
= &hvc_opal_raw_ops
;
166 } else if (of_device_is_compatible(dev
->dev
.of_node
,
167 "ibm,opal-console-hvsi")) {
168 proto
= HV_PROTOCOL_HVSI
;
169 ops
= &hvc_opal_hvsi_ops
;
171 pr_err("hvc_opal: Unknown protocol for %pOF\n",
176 reg
= of_get_property(dev
->dev
.of_node
, "reg", NULL
);
177 termno
= reg
? be32_to_cpup(reg
) : 0;
179 /* Is it our boot one ? */
180 if (hvc_opal_privs
[termno
] == &hvc_opal_boot_priv
) {
181 pv
= hvc_opal_privs
[termno
];
183 } else if (hvc_opal_privs
[termno
] == NULL
) {
184 pv
= kzalloc(sizeof(struct hvc_opal_priv
), GFP_KERNEL
);
188 hvc_opal_privs
[termno
] = pv
;
189 if (proto
== HV_PROTOCOL_HVSI
) {
191 * We want put_chars to be atomic to avoid mangling of
194 hvsilib_init(&pv
->hvsi
,
195 opal_get_chars
, opal_put_chars_atomic
,
199 /* Instanciate now to establish a mapping index==vtermno */
200 hvc_instantiate(termno
, termno
, ops
);
202 pr_err("hvc_opal: Device %pOF has duplicate terminal number #%d\n",
203 dev
->dev
.of_node
, termno
);
207 pr_info("hvc%d: %s protocol on %pOF%s\n", termno
,
208 proto
== HV_PROTOCOL_RAW
? "raw" : "hvsi",
210 boot
? " (boot console)" : "");
212 irq
= irq_of_parse_and_map(dev
->dev
.of_node
, 0);
214 pr_info("hvc%d: No interrupts property, using OPAL event\n",
216 irq
= opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT
));
220 pr_err("hvc_opal: Unable to map interrupt for device %pOF\n",
225 hp
= hvc_alloc(termno
, irq
, ops
, MAX_VIO_PUT_CHARS
);
229 /* hvc consoles on powernv may need to share a single irq */
230 hp
->flags
= IRQF_SHARED
;
231 dev_set_drvdata(&dev
->dev
, hp
);
236 static void hvc_opal_remove(struct platform_device
*dev
)
238 struct hvc_struct
*hp
= dev_get_drvdata(&dev
->dev
);
241 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
;
248 static struct platform_driver hvc_opal_driver
= {
249 .probe
= hvc_opal_probe
,
250 .remove
= hvc_opal_remove
,
252 .name
= hvc_opal_name
,
253 .of_match_table
= hvc_opal_match
,
257 static int __init
hvc_opal_init(void)
259 if (!firmware_has_feature(FW_FEATURE_OPAL
))
262 /* Register as a vio device to receive callbacks */
263 return platform_driver_register(&hvc_opal_driver
);
265 device_initcall(hvc_opal_init
);
267 static void udbg_opal_putc(char c
)
269 unsigned int termno
= hvc_opal_boot_termno
;
273 udbg_opal_putc('\r');
276 switch(hvc_opal_boot_priv
.proto
) {
277 case HV_PROTOCOL_RAW
:
278 count
= opal_put_chars(termno
, &c
, 1);
280 case HV_PROTOCOL_HVSI
:
281 count
= hvc_opal_hvsi_put_chars(termno
, &c
, 1);
285 /* This is needed for the cosole to flush
286 * when there aren't any interrupts.
288 opal_flush_console(termno
);
289 } while(count
== 0 || count
== -EAGAIN
);
292 static int udbg_opal_getc_poll(void)
294 unsigned int termno
= hvc_opal_boot_termno
;
298 switch(hvc_opal_boot_priv
.proto
) {
299 case HV_PROTOCOL_RAW
:
300 rc
= opal_get_chars(termno
, &c
, 1);
302 case HV_PROTOCOL_HVSI
:
303 rc
= hvc_opal_hvsi_get_chars(termno
, &c
, 1);
311 static int udbg_opal_getc(void)
315 ch
= udbg_opal_getc_poll();
321 static void udbg_init_opal_common(void)
323 udbg_putc
= udbg_opal_putc
;
324 udbg_getc
= udbg_opal_getc
;
325 udbg_getc_poll
= udbg_opal_getc_poll
;
328 void __init
hvc_opal_init_early(void)
330 struct device_node
*stdout_node
= of_node_get(of_stdout
);
331 const __be32
*termno
;
332 const struct hv_ops
*ops
;
335 /* If the console wasn't in /chosen, try /ibm,opal */
337 struct device_node
*opal
, *np
;
339 /* Current OPAL takeover doesn't provide the stdout
340 * path, so we hard wire it
342 opal
= of_find_node_by_path("/ibm,opal/consoles");
344 pr_devel("hvc_opal: Found consoles in new location\n");
346 opal
= of_find_node_by_path("/ibm,opal");
348 pr_devel("hvc_opal: "
349 "Found consoles in old location\n");
353 for_each_child_of_node(opal
, np
) {
354 if (of_node_name_eq(np
, "serial")) {
363 termno
= of_get_property(stdout_node
, "reg", NULL
);
364 index
= termno
? be32_to_cpup(termno
) : 0;
365 if (index
>= MAX_NR_HVC_CONSOLES
)
367 hvc_opal_privs
[index
] = &hvc_opal_boot_priv
;
369 /* Check the protocol */
370 if (of_device_is_compatible(stdout_node
, "ibm,opal-console-raw")) {
371 hvc_opal_boot_priv
.proto
= HV_PROTOCOL_RAW
;
372 ops
= &hvc_opal_raw_ops
;
373 pr_devel("hvc_opal: Found RAW console\n");
375 else if (of_device_is_compatible(stdout_node
,"ibm,opal-console-hvsi")) {
376 hvc_opal_boot_priv
.proto
= HV_PROTOCOL_HVSI
;
377 ops
= &hvc_opal_hvsi_ops
;
378 hvsilib_init(&hvc_opal_boot_priv
.hvsi
,
379 opal_get_chars
, opal_put_chars_atomic
,
381 /* HVSI, perform the handshake now */
382 hvsilib_establish(&hvc_opal_boot_priv
.hvsi
);
383 pr_devel("hvc_opal: Found HVSI console\n");
386 hvc_opal_boot_termno
= index
;
387 udbg_init_opal_common();
388 add_preferred_console("hvc", index
, NULL
);
389 hvc_instantiate(index
, index
, ops
);
391 of_node_put(stdout_node
);
394 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
395 void __init
udbg_init_debug_opal_raw(void)
397 u32 index
= CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO
;
398 hvc_opal_privs
[index
] = &hvc_opal_boot_priv
;
399 hvc_opal_boot_priv
.proto
= HV_PROTOCOL_RAW
;
400 hvc_opal_boot_termno
= index
;
401 udbg_init_opal_common();
403 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
405 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
406 void __init
udbg_init_debug_opal_hvsi(void)
408 u32 index
= CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO
;
409 hvc_opal_privs
[index
] = &hvc_opal_boot_priv
;
410 hvc_opal_boot_termno
= index
;
411 udbg_init_opal_common();
412 hvsilib_init(&hvc_opal_boot_priv
.hvsi
,
413 opal_get_chars
, opal_put_chars_atomic
,
415 hvsilib_establish(&hvc_opal_boot_priv
.hvsi
);
417 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */