2 * opal driver interface to hvc_console.c
4 * Copyright 2011 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
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>
32 #include <asm/hvconsole.h>
34 #include <asm/firmware.h>
39 #include "hvc_console.h"
41 static const char hvc_opal_name[] = "hvc_opal";
43 static struct of_device_id hvc_opal_match[] __devinitdata = {
44 { .name = "serial", .compatible = "ibm,opal-console-raw" },
45 { .name = "serial", .compatible = "ibm,opal-console-hvsi" },
49 typedef enum hv_protocol {
54 struct hvc_opal_priv {
55 hv_protocol_t proto; /* Raw data or HVSI packets */
56 struct hvsi_priv hvsi; /* HVSI specific data */
58 static struct hvc_opal_priv *hvc_opal_privs[MAX_NR_HVC_CONSOLES];
60 /* For early boot console */
61 static struct hvc_opal_priv hvc_opal_boot_priv;
62 static u32 hvc_opal_boot_termno;
64 static const struct hv_ops hvc_opal_raw_ops = {
65 .get_chars = opal_get_chars,
66 .put_chars = opal_put_chars,
67 .notifier_add = notifier_add_irq,
68 .notifier_del = notifier_del_irq,
69 .notifier_hangup = notifier_hangup_irq,
72 static int hvc_opal_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
74 struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
79 return hvsilib_get_chars(&pv->hvsi, buf, count);
82 static int hvc_opal_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
84 struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
89 return hvsilib_put_chars(&pv->hvsi, buf, count);
92 static int hvc_opal_hvsi_open(struct hvc_struct *hp, int data)
94 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
97 pr_devel("HVSI@%x: do open !\n", hp->vtermno);
99 rc = notifier_add_irq(hp, data);
103 return hvsilib_open(&pv->hvsi, hp);
106 static void hvc_opal_hvsi_close(struct hvc_struct *hp, int data)
108 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
110 pr_devel("HVSI@%x: do close !\n", hp->vtermno);
112 hvsilib_close(&pv->hvsi, hp);
114 notifier_del_irq(hp, data);
117 void hvc_opal_hvsi_hangup(struct hvc_struct *hp, int data)
119 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
121 pr_devel("HVSI@%x: do hangup !\n", hp->vtermno);
123 hvsilib_close(&pv->hvsi, hp);
125 notifier_hangup_irq(hp, data);
128 static int hvc_opal_hvsi_tiocmget(struct hvc_struct *hp)
130 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
134 return pv->hvsi.mctrl;
137 static int hvc_opal_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
140 struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
142 pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
143 hp->vtermno, set, clear);
146 hvsilib_write_mctrl(&pv->hvsi, 1);
147 else if (clear & TIOCM_DTR)
148 hvsilib_write_mctrl(&pv->hvsi, 0);
153 static const struct hv_ops hvc_opal_hvsi_ops = {
154 .get_chars = hvc_opal_hvsi_get_chars,
155 .put_chars = hvc_opal_hvsi_put_chars,
156 .notifier_add = hvc_opal_hvsi_open,
157 .notifier_del = hvc_opal_hvsi_close,
158 .notifier_hangup = hvc_opal_hvsi_hangup,
159 .tiocmget = hvc_opal_hvsi_tiocmget,
160 .tiocmset = hvc_opal_hvsi_tiocmset,
163 static int __devinit hvc_opal_probe(struct platform_device *dev)
165 const struct hv_ops *ops;
166 struct hvc_struct *hp;
167 struct hvc_opal_priv *pv;
169 unsigned int termno, boot = 0;
172 if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) {
173 proto = HV_PROTOCOL_RAW;
174 ops = &hvc_opal_raw_ops;
175 } else if (of_device_is_compatible(dev->dev.of_node,
176 "ibm,opal-console-hvsi")) {
177 proto = HV_PROTOCOL_HVSI;
178 ops = &hvc_opal_hvsi_ops;
180 pr_err("hvc_opal: Unkown protocol for %s\n",
181 dev->dev.of_node->full_name);
185 reg = of_get_property(dev->dev.of_node, "reg", NULL);
186 termno = reg ? be32_to_cpup(reg) : 0;
188 /* Is it our boot one ? */
189 if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
190 pv = hvc_opal_privs[termno];
192 } else if (hvc_opal_privs[termno] == NULL) {
193 pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL);
197 hvc_opal_privs[termno] = pv;
198 if (proto == HV_PROTOCOL_HVSI)
199 hvsilib_init(&pv->hvsi, opal_get_chars, opal_put_chars,
202 /* Instanciate now to establish a mapping index==vtermno */
203 hvc_instantiate(termno, termno, ops);
205 pr_err("hvc_opal: Device %s has duplicate terminal number #%d\n",
206 dev->dev.of_node->full_name, termno);
210 pr_info("hvc%d: %s protocol on %s%s\n", termno,
211 proto == HV_PROTOCOL_RAW ? "raw" : "hvsi",
212 dev->dev.of_node->full_name,
213 boot ? " (boot console)" : "");
215 /* We don't do IRQ yet */
216 hp = hvc_alloc(termno, 0, ops, MAX_VIO_PUT_CHARS);
219 dev_set_drvdata(&dev->dev, hp);
224 static int __devexit hvc_opal_remove(struct platform_device *dev)
226 struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
229 termno = hp->vtermno;
232 if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
233 kfree(hvc_opal_privs[termno]);
234 hvc_opal_privs[termno] = NULL;
239 static struct platform_driver hvc_opal_driver = {
240 .probe = hvc_opal_probe,
241 .remove = __devexit_p(hvc_opal_remove),
243 .name = hvc_opal_name,
244 .owner = THIS_MODULE,
245 .of_match_table = hvc_opal_match,
249 static int __init hvc_opal_init(void)
251 if (!firmware_has_feature(FW_FEATURE_OPAL))
254 /* Register as a vio device to receive callbacks */
255 return platform_driver_register(&hvc_opal_driver);
257 module_init(hvc_opal_init);
259 static void __exit hvc_opal_exit(void)
261 platform_driver_unregister(&hvc_opal_driver);
263 module_exit(hvc_opal_exit);
265 static void udbg_opal_putc(char c)
267 unsigned int termno = hvc_opal_boot_termno;
271 udbg_opal_putc('\r');
274 switch(hvc_opal_boot_priv.proto) {
275 case HV_PROTOCOL_RAW:
276 count = opal_put_chars(termno, &c, 1);
278 case HV_PROTOCOL_HVSI:
279 count = hvc_opal_hvsi_put_chars(termno, &c, 1);
282 } while(count == 0 || count == -EAGAIN);
285 static int udbg_opal_getc_poll(void)
287 unsigned int termno = hvc_opal_boot_termno;
291 switch(hvc_opal_boot_priv.proto) {
292 case HV_PROTOCOL_RAW:
293 rc = opal_get_chars(termno, &c, 1);
295 case HV_PROTOCOL_HVSI:
296 rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
304 static int udbg_opal_getc(void)
308 ch = udbg_opal_getc_poll();
310 /* This shouldn't be needed...but... */
311 volatile unsigned long delay;
312 for (delay=0; delay < 2000000; delay++)
320 static void udbg_init_opal_common(void)
322 udbg_putc = udbg_opal_putc;
323 udbg_getc = udbg_opal_getc;
324 udbg_getc_poll = udbg_opal_getc_poll;
325 tb_ticks_per_usec = 0x200; /* Make udelay not suck */
328 void __init hvc_opal_init_early(void)
330 struct device_node *stdout_node = NULL;
332 const char *name = NULL;
333 const struct hv_ops *ops;
336 /* find the boot console from /chosen/stdout */
338 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
340 stdout_node = of_find_node_by_path(name);
342 pr_err("hvc_opal: Failed to locate default console!\n");
346 struct device_node *opal, *np;
348 /* Current OPAL takeover doesn't provide the stdout
349 * path, so we hard wire it
351 opal = of_find_node_by_path("/ibm,opal/consoles");
353 pr_devel("hvc_opal: Found consoles in new location\n");
355 opal = of_find_node_by_path("/ibm,opal");
357 pr_devel("hvc_opal: "
358 "Found consoles in old location\n");
362 for_each_child_of_node(opal, np) {
363 if (!strcmp(np->name, "serial")) {
372 termno = of_get_property(stdout_node, "reg", NULL);
373 index = termno ? *termno : 0;
374 if (index >= MAX_NR_HVC_CONSOLES)
376 hvc_opal_privs[index] = &hvc_opal_boot_priv;
378 /* Check the protocol */
379 if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
380 hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
381 ops = &hvc_opal_raw_ops;
382 pr_devel("hvc_opal: Found RAW console\n");
384 else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
385 hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
386 ops = &hvc_opal_hvsi_ops;
387 hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars,
388 opal_put_chars, index, 1);
389 /* HVSI, perform the handshake now */
390 hvsilib_establish(&hvc_opal_boot_priv.hvsi);
391 pr_devel("hvc_opal: Found HVSI console\n");
394 hvc_opal_boot_termno = index;
395 udbg_init_opal_common();
396 add_preferred_console("hvc", index, NULL);
397 hvc_instantiate(index, index, ops);
399 of_node_put(stdout_node);
402 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
403 void __init udbg_init_debug_opal(void)
405 u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
406 hvc_opal_privs[index] = &hvc_opal_boot_priv;
407 hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
408 hvc_opal_boot_termno = index;
409 udbg_init_opal_common();
411 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
413 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
414 void __init udbg_init_debug_opal_hvsi(void)
416 u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
417 hvc_opal_privs[index] = &hvc_opal_boot_priv;
418 hvc_opal_boot_termno = index;
419 udbg_init_opal_common();
420 hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars, opal_put_chars,
422 hvsilib_establish(&hvc_opal_boot_priv.hvsi);
424 #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */