Commit | Line | Data |
---|---|---|
14a43e69 BH |
1 | /* |
2 | * PowerNV OPAL high level interfaces | |
3 | * | |
4 | * Copyright 2011 IBM Corp. | |
5 | * | |
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. | |
10 | */ | |
11 | ||
12 | #undef DEBUG | |
13 | ||
14 | #include <linux/types.h> | |
15 | #include <linux/of.h> | |
16 | #include <linux/of_platform.h> | |
a125e092 | 17 | #include <linux/interrupt.h> |
14a43e69 BH |
18 | #include <asm/opal.h> |
19 | #include <asm/firmware.h> | |
20 | ||
21 | #include "powernv.h" | |
22 | ||
23 | struct opal { | |
24 | u64 base; | |
25 | u64 entry; | |
26 | } opal; | |
27 | ||
28 | static struct device_node *opal_node; | |
29 | static DEFINE_SPINLOCK(opal_write_lock); | |
30 | ||
31 | int __init early_init_dt_scan_opal(unsigned long node, | |
32 | const char *uname, int depth, void *data) | |
33 | { | |
34 | const void *basep, *entryp; | |
35 | unsigned long basesz, entrysz; | |
36 | ||
37 | if (depth != 1 || strcmp(uname, "ibm,opal") != 0) | |
38 | return 0; | |
39 | ||
40 | basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz); | |
41 | entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz); | |
42 | ||
43 | if (!basep || !entryp) | |
44 | return 1; | |
45 | ||
46 | opal.base = of_read_number(basep, basesz/4); | |
47 | opal.entry = of_read_number(entryp, entrysz/4); | |
48 | ||
49 | pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n", | |
50 | opal.base, basep, basesz); | |
51 | pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n", | |
52 | opal.entry, entryp, entrysz); | |
53 | ||
54 | powerpc_firmware_features |= FW_FEATURE_OPAL; | |
55 | if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) { | |
56 | powerpc_firmware_features |= FW_FEATURE_OPALv2; | |
57 | printk("OPAL V2 detected !\n"); | |
58 | } else { | |
59 | printk("OPAL V1 detected !\n"); | |
60 | } | |
61 | ||
62 | return 1; | |
63 | } | |
64 | ||
65 | int opal_get_chars(uint32_t vtermno, char *buf, int count) | |
66 | { | |
67 | s64 len, rc; | |
68 | u64 evt; | |
69 | ||
70 | if (!opal.entry) | |
daea1175 | 71 | return -ENODEV; |
14a43e69 BH |
72 | opal_poll_events(&evt); |
73 | if ((evt & OPAL_EVENT_CONSOLE_INPUT) == 0) | |
74 | return 0; | |
75 | len = count; | |
76 | rc = opal_console_read(vtermno, &len, buf); | |
77 | if (rc == OPAL_SUCCESS) | |
78 | return len; | |
79 | return 0; | |
80 | } | |
81 | ||
82 | int opal_put_chars(uint32_t vtermno, const char *data, int total_len) | |
83 | { | |
84 | int written = 0; | |
daea1175 | 85 | s64 len, rc; |
14a43e69 BH |
86 | unsigned long flags; |
87 | u64 evt; | |
88 | ||
89 | if (!opal.entry) | |
daea1175 | 90 | return -ENODEV; |
14a43e69 BH |
91 | |
92 | /* We want put_chars to be atomic to avoid mangling of hvsi | |
93 | * packets. To do that, we first test for room and return | |
daea1175 BH |
94 | * -EAGAIN if there isn't enough. |
95 | * | |
96 | * Unfortunately, opal_console_write_buffer_space() doesn't | |
97 | * appear to work on opal v1, so we just assume there is | |
98 | * enough room and be done with it | |
14a43e69 BH |
99 | */ |
100 | spin_lock_irqsave(&opal_write_lock, flags); | |
daea1175 BH |
101 | if (firmware_has_feature(FW_FEATURE_OPALv2)) { |
102 | rc = opal_console_write_buffer_space(vtermno, &len); | |
103 | if (rc || len < total_len) { | |
104 | spin_unlock_irqrestore(&opal_write_lock, flags); | |
105 | /* Closed -> drop characters */ | |
106 | if (rc) | |
107 | return total_len; | |
108 | opal_poll_events(&evt); | |
109 | return -EAGAIN; | |
110 | } | |
14a43e69 BH |
111 | } |
112 | ||
113 | /* We still try to handle partial completions, though they | |
114 | * should no longer happen. | |
115 | */ | |
daea1175 | 116 | rc = OPAL_BUSY; |
14a43e69 BH |
117 | while(total_len > 0 && (rc == OPAL_BUSY || |
118 | rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) { | |
119 | len = total_len; | |
120 | rc = opal_console_write(vtermno, &len, data); | |
121 | if (rc == OPAL_SUCCESS) { | |
122 | total_len -= len; | |
123 | data += len; | |
124 | written += len; | |
125 | } | |
126 | /* This is a bit nasty but we need that for the console to | |
127 | * flush when there aren't any interrupts. We will clean | |
128 | * things a bit later to limit that to synchronous path | |
129 | * such as the kernel console and xmon/udbg | |
130 | */ | |
131 | do | |
132 | opal_poll_events(&evt); | |
133 | while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT)); | |
134 | } | |
135 | spin_unlock_irqrestore(&opal_write_lock, flags); | |
136 | return written; | |
137 | } | |
138 | ||
a125e092 BH |
139 | static irqreturn_t opal_interrupt(int irq, void *data) |
140 | { | |
141 | uint64_t events; | |
142 | ||
143 | opal_handle_interrupt(virq_to_hw(irq), &events); | |
144 | ||
145 | /* XXX TODO: Do something with the events */ | |
146 | ||
147 | return IRQ_HANDLED; | |
148 | } | |
149 | ||
14a43e69 BH |
150 | static int __init opal_init(void) |
151 | { | |
152 | struct device_node *np, *consoles; | |
a125e092 BH |
153 | const u32 *irqs; |
154 | int rc, i, irqlen; | |
14a43e69 BH |
155 | |
156 | opal_node = of_find_node_by_path("/ibm,opal"); | |
157 | if (!opal_node) { | |
158 | pr_warn("opal: Node not found\n"); | |
159 | return -ENODEV; | |
160 | } | |
161 | if (firmware_has_feature(FW_FEATURE_OPALv2)) | |
162 | consoles = of_find_node_by_path("/ibm,opal/consoles"); | |
163 | else | |
164 | consoles = of_node_get(opal_node); | |
165 | ||
166 | /* Register serial ports */ | |
167 | for_each_child_of_node(consoles, np) { | |
168 | if (strcmp(np->name, "serial")) | |
169 | continue; | |
170 | of_platform_device_create(np, NULL, NULL); | |
171 | } | |
172 | of_node_put(consoles); | |
a125e092 BH |
173 | |
174 | /* Find all OPAL interrupts and request them */ | |
175 | irqs = of_get_property(opal_node, "opal-interrupts", &irqlen); | |
176 | pr_debug("opal: Found %d interrupts reserved for OPAL\n", | |
177 | irqs ? (irqlen / 4) : 0); | |
178 | for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) { | |
179 | unsigned int hwirq = be32_to_cpup(irqs); | |
180 | unsigned int irq = irq_create_mapping(NULL, hwirq); | |
181 | if (irq == NO_IRQ) { | |
182 | pr_warning("opal: Failed to map irq 0x%x\n", hwirq); | |
183 | continue; | |
184 | } | |
185 | rc = request_irq(irq, opal_interrupt, 0, "opal", NULL); | |
186 | if (rc) | |
187 | pr_warning("opal: Error %d requesting irq %d" | |
188 | " (0x%x)\n", rc, irq, hwirq); | |
189 | } | |
14a43e69 BH |
190 | return 0; |
191 | } | |
192 | subsys_initcall(opal_init); |