2 * opal driver interface to hvc_console.c
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/console.h>
30 #include <linux/of_platform.h>
31 #include <linux/export.h>
32 #include <linux/interrupt.h>
34 #include <asm/hvconsole.h>
36 #include <asm/firmware.h>
41 #include "hvc_console.h"
43 static const char hvc_opal_name[] = "hvc_opal";
45 static const struct of_device_id hvc_opal_match[] = {
46 { .name = "serial", .compatible = "ibm,opal-console-raw" },
47 { .name = "serial", .compatible = "ibm,opal-console-hvsi" },
51 typedef enum hv_protocol {
56 struct hvc_opal_priv {
57 hv_protocol_t proto; /* Raw data or HVSI packets */
58 struct hvsi_priv hvsi; /* HVSI specific data */
60 static struct hvc_opal_priv *hvc_opal_privs[MAX_NR_HVC_CONSOLES];
62 /* For early boot console */
63 static struct hvc_opal_priv hvc_opal_boot_priv;
64 static u32 hvc_opal_boot_termno;
66 static const struct hv_ops hvc_opal_raw_ops = {
67 .get_chars = opal_get_chars,
68 .put_chars = opal_put_chars,
69 .notifier_add = notifier_add_irq,
70 .notifier_del = notifier_del_irq,
71 .notifier_hangup = notifier_hangup_irq,
74 static int hvc_opal_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
76 struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
81 return hvsilib_get_chars(&pv->hvsi, buf, count);
84 static int hvc_opal_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
86 struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
91 return hvsilib_put_chars(&pv->hvsi, buf, count);
94 static int hvc_opal_hvsi_open(struct hvc_struct *hp, int data)
96 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
99 pr_devel("HVSI@%x: do open !\n", hp->vtermno);
101 rc = notifier_add_irq(hp, data);
105 return hvsilib_open(&pv->hvsi, hp);
108 static void hvc_opal_hvsi_close(struct hvc_struct *hp, int data)
110 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
112 pr_devel("HVSI@%x: do close !\n", hp->vtermno);
114 hvsilib_close(&pv->hvsi, hp);
116 notifier_del_irq(hp, data);
119 void hvc_opal_hvsi_hangup(struct hvc_struct *hp, int data)
121 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
123 pr_devel("HVSI@%x: do hangup !\n", hp->vtermno);
125 hvsilib_close(&pv->hvsi, hp);
127 notifier_hangup_irq(hp, data);
130 static int hvc_opal_hvsi_tiocmget(struct hvc_struct *hp)
132 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
136 return pv->hvsi.mctrl;
139 static int hvc_opal_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
142 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
144 pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
145 hp->vtermno, set, clear);
148 hvsilib_write_mctrl(&pv->hvsi, 1);
149 else if (clear & TIOCM_DTR)
150 hvsilib_write_mctrl(&pv->hvsi, 0);
155 static const struct hv_ops hvc_opal_hvsi_ops = {
156 .get_chars = hvc_opal_hvsi_get_chars,
157 .put_chars = hvc_opal_hvsi_put_chars,
158 .notifier_add = hvc_opal_hvsi_open,
159 .notifier_del = hvc_opal_hvsi_close,
160 .notifier_hangup = hvc_opal_hvsi_hangup,
161 .tiocmget = hvc_opal_hvsi_tiocmget,
162 .tiocmset = hvc_opal_hvsi_tiocmset,
165 static int hvc_opal_probe(struct platform_device *dev)
167 const struct hv_ops *ops;
168 struct hvc_struct *hp;
169 struct hvc_opal_priv *pv;
171 unsigned int termno, irq, boot = 0;
174 if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) {
175 proto = HV_PROTOCOL_RAW;
176 ops = &hvc_opal_raw_ops;
177 } else if (of_device_is_compatible(dev->dev.of_node,
178 "ibm,opal-console-hvsi")) {
179 proto = HV_PROTOCOL_HVSI;
180 ops = &hvc_opal_hvsi_ops;
182 pr_err("hvc_opal: Unknown protocol for %s\n",
183 dev->dev.of_node->full_name);
187 reg = of_get_property(dev->dev.of_node, "reg", NULL);
188 termno = reg ? be32_to_cpup(reg) : 0;
190 /* Is it our boot one ? */
191 if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
192 pv = hvc_opal_privs[termno];
194 } else if (hvc_opal_privs[termno] == NULL) {
195 pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL);
199 hvc_opal_privs[termno] = pv;
200 if (proto == HV_PROTOCOL_HVSI)
201 hvsilib_init(&pv->hvsi, opal_get_chars, opal_put_chars,
204 /* Instanciate now to establish a mapping index==vtermno */
205 hvc_instantiate(termno, termno, ops);
207 pr_err("hvc_opal: Device %s has duplicate terminal number #%d\n",
208 dev->dev.of_node->full_name, termno);
212 pr_info("hvc%d: %s protocol on %s%s\n", termno,
213 proto == HV_PROTOCOL_RAW ? "raw" : "hvsi",
214 dev->dev.of_node->full_name,
215 boot ? " (boot console)" : "");
217 irq = opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT));
219 pr_err("hvc_opal: Unable to map interrupt for device %s\n",
220 dev->dev.of_node->full_name);
224 hp = hvc_alloc(termno, irq, ops, MAX_VIO_PUT_CHARS);
227 dev_set_drvdata(&dev->dev, hp);
232 static int hvc_opal_remove(struct platform_device *dev)
234 struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
237 termno = hp->vtermno;
240 if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
241 kfree(hvc_opal_privs[termno]);
242 hvc_opal_privs[termno] = NULL;
247 static struct platform_driver hvc_opal_driver = {
248 .probe = hvc_opal_probe,
249 .remove = hvc_opal_remove,
251 .name = hvc_opal_name,
252 .of_match_table = hvc_opal_match,
256 static int __init hvc_opal_init(void)
258 if (!firmware_has_feature(FW_FEATURE_OPAL))
261 /* Register as a vio device to receive callbacks */
262 return platform_driver_register(&hvc_opal_driver);
264 device_initcall(hvc_opal_init);
266 static void udbg_opal_putc(char c)
268 unsigned int termno = hvc_opal_boot_termno;
272 udbg_opal_putc('\r');
275 switch(hvc_opal_boot_priv.proto) {
276 case HV_PROTOCOL_RAW:
277 count = opal_put_chars(termno, &c, 1);
279 case HV_PROTOCOL_HVSI:
280 count = hvc_opal_hvsi_put_chars(termno, &c, 1);
283 } while(count == 0 || count == -EAGAIN);
286 static int udbg_opal_getc_poll(void)
288 unsigned int termno = hvc_opal_boot_termno;
292 switch(hvc_opal_boot_priv.proto) {
293 case HV_PROTOCOL_RAW:
294 rc = opal_get_chars(termno, &c, 1);
296 case HV_PROTOCOL_HVSI:
297 rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
305 static int udbg_opal_getc(void)
309 ch = udbg_opal_getc_poll();
311 /* This shouldn't be needed...but... */
312 volatile unsigned long delay;
313 for (delay=0; delay < 2000000; delay++)
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;
326 tb_ticks_per_usec = 0x200; /* Make udelay not suck */
329 void __init hvc_opal_init_early(void)
331 struct device_node *stdout_node = of_node_get(of_stdout);
332 const __be32 *termno;
333 const struct hv_ops *ops;
336 /* If the console wasn't in /chosen, try /ibm,opal */
338 struct device_node *opal, *np;
340 /* Current OPAL takeover doesn't provide the stdout
341 * path, so we hard wire it
343 opal = of_find_node_by_path("/ibm,opal/consoles");
345 pr_devel("hvc_opal: Found consoles in new location\n");
347 opal = of_find_node_by_path("/ibm,opal");
349 pr_devel("hvc_opal: "
350 "Found consoles in old location\n");
354 for_each_child_of_node(opal, np) {
355 if (!strcmp(np->name, "serial")) {
364 termno = of_get_property(stdout_node, "reg", NULL);
365 index = termno ? be32_to_cpup(termno) : 0;
366 if (index >= MAX_NR_HVC_CONSOLES)
368 hvc_opal_privs[index] = &hvc_opal_boot_priv;
370 /* Check the protocol */
371 if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
372 hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
373 ops = &hvc_opal_raw_ops;
374 pr_devel("hvc_opal: Found RAW console\n");
376 else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
377 hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
378 ops = &hvc_opal_hvsi_ops;
379 hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars,
380 opal_put_chars, index, 1);
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, opal_get_chars, opal_put_chars,
414 hvsilib_establish(&hvc_opal_boot_priv.hvsi);
416 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */