2 * PowerNV OPAL high level interfaces
4 * Copyright 2011 IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
14 #include <linux/types.h>
16 #include <linux/of_platform.h>
18 #include <asm/firmware.h>
27 static struct device_node *opal_node;
28 static DEFINE_SPINLOCK(opal_write_lock);
30 int __init early_init_dt_scan_opal(unsigned long node,
31 const char *uname, int depth, void *data)
33 const void *basep, *entryp;
34 unsigned long basesz, entrysz;
36 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
39 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
40 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
42 if (!basep || !entryp)
45 opal.base = of_read_number(basep, basesz/4);
46 opal.entry = of_read_number(entryp, entrysz/4);
48 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
49 opal.base, basep, basesz);
50 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
51 opal.entry, entryp, entrysz);
53 powerpc_firmware_features |= FW_FEATURE_OPAL;
54 if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
55 powerpc_firmware_features |= FW_FEATURE_OPALv2;
56 printk("OPAL V2 detected !\n");
58 printk("OPAL V1 detected !\n");
64 int opal_get_chars(uint32_t vtermno, char *buf, int count)
71 opal_poll_events(&evt);
72 if ((evt & OPAL_EVENT_CONSOLE_INPUT) == 0)
75 rc = opal_console_read(vtermno, &len, buf);
76 if (rc == OPAL_SUCCESS)
81 int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
91 /* We want put_chars to be atomic to avoid mangling of hvsi
92 * packets. To do that, we first test for room and return
93 * -EAGAIN if there isn't enough.
95 * Unfortunately, opal_console_write_buffer_space() doesn't
96 * appear to work on opal v1, so we just assume there is
97 * enough room and be done with it
99 spin_lock_irqsave(&opal_write_lock, flags);
100 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
101 rc = opal_console_write_buffer_space(vtermno, &len);
102 if (rc || len < total_len) {
103 spin_unlock_irqrestore(&opal_write_lock, flags);
104 /* Closed -> drop characters */
107 opal_poll_events(&evt);
112 /* We still try to handle partial completions, though they
113 * should no longer happen.
116 while(total_len > 0 && (rc == OPAL_BUSY ||
117 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
119 rc = opal_console_write(vtermno, &len, data);
120 if (rc == OPAL_SUCCESS) {
125 /* This is a bit nasty but we need that for the console to
126 * flush when there aren't any interrupts. We will clean
127 * things a bit later to limit that to synchronous path
128 * such as the kernel console and xmon/udbg
131 opal_poll_events(&evt);
132 while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT));
134 spin_unlock_irqrestore(&opal_write_lock, flags);
138 static int __init opal_init(void)
140 struct device_node *np, *consoles;
142 opal_node = of_find_node_by_path("/ibm,opal");
144 pr_warn("opal: Node not found\n");
147 if (firmware_has_feature(FW_FEATURE_OPALv2))
148 consoles = of_find_node_by_path("/ibm,opal/consoles");
150 consoles = of_node_get(opal_node);
152 /* Register serial ports */
153 for_each_child_of_node(consoles, np) {
154 if (strcmp(np->name, "serial"))
156 of_platform_device_create(np, NULL, NULL);
158 of_node_put(consoles);
161 subsys_initcall(opal_init);