Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * opal driver interface to hvc_console.c
  3 *
  4 * Copyright 2011 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  5 *
  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.
 10 *
 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.
 15 *
 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
 19 *
 20 */
 21
 22#undef DEBUG
 23
 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>
 29#include <linux/of.h>
 30#include <linux/of_platform.h>
 31#include <linux/export.h>
 32#include <linux/interrupt.h>
 33
 34#include <asm/hvconsole.h>
 35#include <asm/prom.h>
 36#include <asm/firmware.h>
 37#include <asm/hvsi.h>
 38#include <asm/udbg.h>
 39#include <asm/opal.h>
 40
 41#include "hvc_console.h"
 42
 43static const char hvc_opal_name[] = "hvc_opal";
 44
 45static const struct of_device_id hvc_opal_match[] = {
 46	{ .name = "serial", .compatible = "ibm,opal-console-raw" },
 47	{ .name = "serial", .compatible = "ibm,opal-console-hvsi" },
 48	{ },
 49};
 50
 51typedef enum hv_protocol {
 52	HV_PROTOCOL_RAW,
 53	HV_PROTOCOL_HVSI
 54} hv_protocol_t;
 55
 56struct hvc_opal_priv {
 57	hv_protocol_t		proto;	/* Raw data or HVSI packets */
 58	struct hvsi_priv	hvsi;	/* HVSI specific data */
 59};
 60static struct hvc_opal_priv *hvc_opal_privs[MAX_NR_HVC_CONSOLES];
 61
 62/* For early boot console */
 63static struct hvc_opal_priv hvc_opal_boot_priv;
 64static u32 hvc_opal_boot_termno;
 65
 66static 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,
 72};
 73
 74static int hvc_opal_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
 75{
 76	struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
 77
 78	if (WARN_ON(!pv))
 79		return -ENODEV;
 80
 81	return hvsilib_get_chars(&pv->hvsi, buf, count);
 82}
 83
 84static int hvc_opal_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
 85{
 86	struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
 87
 88	if (WARN_ON(!pv))
 89		return -ENODEV;
 90
 91	return hvsilib_put_chars(&pv->hvsi, buf, count);
 92}
 93
 94static int hvc_opal_hvsi_open(struct hvc_struct *hp, int data)
 95{
 96	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
 97	int rc;
 98
 99	pr_devel("HVSI@%x: do open !\n", hp->vtermno);
100
101	rc = notifier_add_irq(hp, data);
102	if (rc)
103		return rc;
104
105	return hvsilib_open(&pv->hvsi, hp);
106}
107
108static void hvc_opal_hvsi_close(struct hvc_struct *hp, int data)
109{
110	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
111
112	pr_devel("HVSI@%x: do close !\n", hp->vtermno);
113
114	hvsilib_close(&pv->hvsi, hp);
115
116	notifier_del_irq(hp, data);
117}
118
119void hvc_opal_hvsi_hangup(struct hvc_struct *hp, int data)
120{
121	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
122
123	pr_devel("HVSI@%x: do hangup !\n", hp->vtermno);
124
125	hvsilib_close(&pv->hvsi, hp);
126
127	notifier_hangup_irq(hp, data);
128}
129
130static int hvc_opal_hvsi_tiocmget(struct hvc_struct *hp)
131{
132	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
133
134	if (!pv)
135		return -EINVAL;
136	return pv->hvsi.mctrl;
137}
138
139static int hvc_opal_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
140				unsigned int clear)
141{
142	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
143
144	pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
145		 hp->vtermno, set, clear);
146
147	if (set & TIOCM_DTR)
148		hvsilib_write_mctrl(&pv->hvsi, 1);
149	else if (clear & TIOCM_DTR)
150		hvsilib_write_mctrl(&pv->hvsi, 0);
151
152	return 0;
153}
154
155static 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,
163};
164
165static int hvc_opal_probe(struct platform_device *dev)
166{
167	const struct hv_ops *ops;
168	struct hvc_struct *hp;
169	struct hvc_opal_priv *pv;
170	hv_protocol_t proto;
171	unsigned int termno, irq, boot = 0;
172	const __be32 *reg;
173
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;
181	} else {
182		pr_err("hvc_opal: Unknown protocol for %s\n",
183		       dev->dev.of_node->full_name);
184		return -ENXIO;
185	}
186
187	reg = of_get_property(dev->dev.of_node, "reg", NULL);
188	termno = reg ? be32_to_cpup(reg) : 0;
189
190	/* Is it our boot one ? */
191	if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
192		pv = hvc_opal_privs[termno];
193		boot = 1;
194	} else if (hvc_opal_privs[termno] == NULL) {
195		pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL);
196		if (!pv)
197			return -ENOMEM;
198		pv->proto = proto;
199		hvc_opal_privs[termno] = pv;
200		if (proto == HV_PROTOCOL_HVSI)
201			hvsilib_init(&pv->hvsi, opal_get_chars, opal_put_chars,
 
 
 
 
 
202				     termno, 0);
 
203
204		/* Instanciate now to establish a mapping index==vtermno */
205		hvc_instantiate(termno, termno, ops);
206	} else {
207		pr_err("hvc_opal: Device %s has duplicate terminal number #%d\n",
208		       dev->dev.of_node->full_name, termno);
209		return -ENXIO;
210	}
211
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)" : "");
216
217	irq = opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT));
 
 
 
 
 
 
218	if (!irq) {
219		pr_err("hvc_opal: Unable to map interrupt for device %s\n",
220			dev->dev.of_node->full_name);
221		return irq;
222	}
223
224	hp = hvc_alloc(termno, irq, ops, MAX_VIO_PUT_CHARS);
225	if (IS_ERR(hp))
226		return PTR_ERR(hp);
 
 
 
227	dev_set_drvdata(&dev->dev, hp);
228
229	return 0;
230}
231
232static int hvc_opal_remove(struct platform_device *dev)
233{
234	struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
235	int rc, termno;
236
237	termno = hp->vtermno;
238	rc = hvc_remove(hp);
239	if (rc == 0) {
240		if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
241			kfree(hvc_opal_privs[termno]);
242		hvc_opal_privs[termno] = NULL;
243	}
244	return rc;
245}
246
247static struct platform_driver hvc_opal_driver = {
248	.probe		= hvc_opal_probe,
249	.remove		= hvc_opal_remove,
250	.driver		= {
251		.name	= hvc_opal_name,
252		.of_match_table	= hvc_opal_match,
253	}
254};
255
256static int __init hvc_opal_init(void)
257{
258	if (!firmware_has_feature(FW_FEATURE_OPAL))
259		return -ENODEV;
260
261	/* Register as a vio device to receive callbacks */
262	return platform_driver_register(&hvc_opal_driver);
263}
264device_initcall(hvc_opal_init);
265
266static void udbg_opal_putc(char c)
267{
268	unsigned int termno = hvc_opal_boot_termno;
269	int count = -1;
270
271	if (c == '\n')
272		udbg_opal_putc('\r');
273
274	do {
275		switch(hvc_opal_boot_priv.proto) {
276		case HV_PROTOCOL_RAW:
277			count = opal_put_chars(termno, &c, 1);
278			break;
279		case HV_PROTOCOL_HVSI:
280			count = hvc_opal_hvsi_put_chars(termno, &c, 1);
281			break;
282		}
 
 
 
 
 
283	} while(count == 0 || count == -EAGAIN);
284}
285
286static int udbg_opal_getc_poll(void)
287{
288	unsigned int termno = hvc_opal_boot_termno;
289	int rc = 0;
290	char c;
291
292	switch(hvc_opal_boot_priv.proto) {
293	case HV_PROTOCOL_RAW:
294		rc = opal_get_chars(termno, &c, 1);
295		break;
296	case HV_PROTOCOL_HVSI:
297		rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
298		break;
299	}
300	if (!rc)
301		return -1;
302	return c;
303}
304
305static int udbg_opal_getc(void)
306{
307	int ch;
308	for (;;) {
309		ch = udbg_opal_getc_poll();
310		if (ch == -1) {
311			/* This shouldn't be needed...but... */
312			volatile unsigned long delay;
313			for (delay=0; delay < 2000000; delay++)
314				;
315		} else {
316			return ch;
317		}
318	}
319}
320
321static void udbg_init_opal_common(void)
322{
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 */
327}
328
329void __init hvc_opal_init_early(void)
330{
331	struct device_node *stdout_node = of_node_get(of_stdout);
332	const __be32 *termno;
333	const struct hv_ops *ops;
334	u32 index;
335
336	/* If the console wasn't in /chosen, try /ibm,opal */
337	if (!stdout_node) {
338		struct device_node *opal, *np;
339
340		/* Current OPAL takeover doesn't provide the stdout
341		 * path, so we hard wire it
342		 */
343		opal = of_find_node_by_path("/ibm,opal/consoles");
344		if (opal)
345			pr_devel("hvc_opal: Found consoles in new location\n");
346		if (!opal) {
347			opal = of_find_node_by_path("/ibm,opal");
348			if (opal)
349				pr_devel("hvc_opal: "
350					 "Found consoles in old location\n");
351		}
352		if (!opal)
353			return;
354		for_each_child_of_node(opal, np) {
355			if (!strcmp(np->name, "serial")) {
356				stdout_node = np;
357				break;
358			}
359		}
360		of_node_put(opal);
361	}
362	if (!stdout_node)
363		return;
364	termno = of_get_property(stdout_node, "reg", NULL);
365	index = termno ? be32_to_cpup(termno) : 0;
366	if (index >= MAX_NR_HVC_CONSOLES)
367		return;
368	hvc_opal_privs[index] = &hvc_opal_boot_priv;
369
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");
375	}
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");
384	} else
385		goto out;
386	hvc_opal_boot_termno = index;
387	udbg_init_opal_common();
388	add_preferred_console("hvc", index, NULL);
389	hvc_instantiate(index, index, ops);
390out:
391	of_node_put(stdout_node);
392}
393
394#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
395void __init udbg_init_debug_opal_raw(void)
396{
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();
402}
403#endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
404
405#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
406void __init udbg_init_debug_opal_hvsi(void)
407{
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,
 
413		     index, 1);
414	hvsilib_establish(&hvc_opal_boot_priv.hvsi);
415}
416#endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */
v5.9
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * opal driver interface to hvc_console.c
  4 *
  5 * Copyright 2011 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#undef DEBUG
  9
 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>
 15#include <linux/of.h>
 16#include <linux/of_platform.h>
 17#include <linux/export.h>
 18#include <linux/interrupt.h>
 19
 20#include <asm/hvconsole.h>
 21#include <asm/prom.h>
 22#include <asm/firmware.h>
 23#include <asm/hvsi.h>
 24#include <asm/udbg.h>
 25#include <asm/opal.h>
 26
 27#include "hvc_console.h"
 28
 29static const char hvc_opal_name[] = "hvc_opal";
 30
 31static const struct of_device_id hvc_opal_match[] = {
 32	{ .name = "serial", .compatible = "ibm,opal-console-raw" },
 33	{ .name = "serial", .compatible = "ibm,opal-console-hvsi" },
 34	{ },
 35};
 36
 37typedef enum hv_protocol {
 38	HV_PROTOCOL_RAW,
 39	HV_PROTOCOL_HVSI
 40} hv_protocol_t;
 41
 42struct hvc_opal_priv {
 43	hv_protocol_t		proto;	/* Raw data or HVSI packets */
 44	struct hvsi_priv	hvsi;	/* HVSI specific data */
 45};
 46static struct hvc_opal_priv *hvc_opal_privs[MAX_NR_HVC_CONSOLES];
 47
 48/* For early boot console */
 49static struct hvc_opal_priv hvc_opal_boot_priv;
 50static u32 hvc_opal_boot_termno;
 51
 52static 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,
 59};
 60
 61static int hvc_opal_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
 62{
 63	struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
 64
 65	if (WARN_ON(!pv))
 66		return -ENODEV;
 67
 68	return hvsilib_get_chars(&pv->hvsi, buf, count);
 69}
 70
 71static int hvc_opal_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
 72{
 73	struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
 74
 75	if (WARN_ON(!pv))
 76		return -ENODEV;
 77
 78	return hvsilib_put_chars(&pv->hvsi, buf, count);
 79}
 80
 81static int hvc_opal_hvsi_open(struct hvc_struct *hp, int data)
 82{
 83	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
 84	int rc;
 85
 86	pr_devel("HVSI@%x: do open !\n", hp->vtermno);
 87
 88	rc = notifier_add_irq(hp, data);
 89	if (rc)
 90		return rc;
 91
 92	return hvsilib_open(&pv->hvsi, hp);
 93}
 94
 95static void hvc_opal_hvsi_close(struct hvc_struct *hp, int data)
 96{
 97	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
 98
 99	pr_devel("HVSI@%x: do close !\n", hp->vtermno);
100
101	hvsilib_close(&pv->hvsi, hp);
102
103	notifier_del_irq(hp, data);
104}
105
106void hvc_opal_hvsi_hangup(struct hvc_struct *hp, int data)
107{
108	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
109
110	pr_devel("HVSI@%x: do hangup !\n", hp->vtermno);
111
112	hvsilib_close(&pv->hvsi, hp);
113
114	notifier_hangup_irq(hp, data);
115}
116
117static int hvc_opal_hvsi_tiocmget(struct hvc_struct *hp)
118{
119	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
120
121	if (!pv)
122		return -EINVAL;
123	return pv->hvsi.mctrl;
124}
125
126static int hvc_opal_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
127				unsigned int clear)
128{
129	struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
130
131	pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
132		 hp->vtermno, set, clear);
133
134	if (set & TIOCM_DTR)
135		hvsilib_write_mctrl(&pv->hvsi, 1);
136	else if (clear & TIOCM_DTR)
137		hvsilib_write_mctrl(&pv->hvsi, 0);
138
139	return 0;
140}
141
142static const struct hv_ops hvc_opal_hvsi_ops = {
143	.get_chars = hvc_opal_hvsi_get_chars,
144	.put_chars = hvc_opal_hvsi_put_chars,
145	.flush = opal_flush_chars,
146	.notifier_add = hvc_opal_hvsi_open,
147	.notifier_del = hvc_opal_hvsi_close,
148	.notifier_hangup = hvc_opal_hvsi_hangup,
149	.tiocmget = hvc_opal_hvsi_tiocmget,
150	.tiocmset = hvc_opal_hvsi_tiocmset,
151};
152
153static int hvc_opal_probe(struct platform_device *dev)
154{
155	const struct hv_ops *ops;
156	struct hvc_struct *hp;
157	struct hvc_opal_priv *pv;
158	hv_protocol_t proto;
159	unsigned int termno, irq, boot = 0;
160	const __be32 *reg;
161
162	if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) {
163		proto = HV_PROTOCOL_RAW;
164		ops = &hvc_opal_raw_ops;
165	} else if (of_device_is_compatible(dev->dev.of_node,
166					   "ibm,opal-console-hvsi")) {
167		proto = HV_PROTOCOL_HVSI;
168		ops = &hvc_opal_hvsi_ops;
169	} else {
170		pr_err("hvc_opal: Unknown protocol for %pOF\n",
171		       dev->dev.of_node);
172		return -ENXIO;
173	}
174
175	reg = of_get_property(dev->dev.of_node, "reg", NULL);
176	termno = reg ? be32_to_cpup(reg) : 0;
177
178	/* Is it our boot one ? */
179	if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
180		pv = hvc_opal_privs[termno];
181		boot = 1;
182	} else if (hvc_opal_privs[termno] == NULL) {
183		pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL);
184		if (!pv)
185			return -ENOMEM;
186		pv->proto = proto;
187		hvc_opal_privs[termno] = pv;
188		if (proto == HV_PROTOCOL_HVSI) {
189			/*
190			 * We want put_chars to be atomic to avoid mangling of
191			 * hvsi packets.
192			 */
193			hvsilib_init(&pv->hvsi,
194				     opal_get_chars, opal_put_chars_atomic,
195				     termno, 0);
196		}
197
198		/* Instanciate now to establish a mapping index==vtermno */
199		hvc_instantiate(termno, termno, ops);
200	} else {
201		pr_err("hvc_opal: Device %pOF has duplicate terminal number #%d\n",
202		       dev->dev.of_node, termno);
203		return -ENXIO;
204	}
205
206	pr_info("hvc%d: %s protocol on %pOF%s\n", termno,
207		proto == HV_PROTOCOL_RAW ? "raw" : "hvsi",
208		dev->dev.of_node,
209		boot ? " (boot console)" : "");
210
211	irq = irq_of_parse_and_map(dev->dev.of_node, 0);
212	if (!irq) {
213		pr_info("hvc%d: No interrupts property, using OPAL event\n",
214				termno);
215		irq = opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT));
216	}
217
218	if (!irq) {
219		pr_err("hvc_opal: Unable to map interrupt for device %pOF\n",
220			dev->dev.of_node);
221		return irq;
222	}
223
224	hp = hvc_alloc(termno, irq, ops, MAX_VIO_PUT_CHARS);
225	if (IS_ERR(hp))
226		return PTR_ERR(hp);
227
228	/* hvc consoles on powernv may need to share a single irq */
229	hp->flags = IRQF_SHARED;
230	dev_set_drvdata(&dev->dev, hp);
231
232	return 0;
233}
234
235static int hvc_opal_remove(struct platform_device *dev)
236{
237	struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
238	int rc, termno;
239
240	termno = hp->vtermno;
241	rc = hvc_remove(hp);
242	if (rc == 0) {
243		if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
244			kfree(hvc_opal_privs[termno]);
245		hvc_opal_privs[termno] = NULL;
246	}
247	return rc;
248}
249
250static struct platform_driver hvc_opal_driver = {
251	.probe		= hvc_opal_probe,
252	.remove		= hvc_opal_remove,
253	.driver		= {
254		.name	= hvc_opal_name,
255		.of_match_table	= hvc_opal_match,
256	}
257};
258
259static int __init hvc_opal_init(void)
260{
261	if (!firmware_has_feature(FW_FEATURE_OPAL))
262		return -ENODEV;
263
264	/* Register as a vio device to receive callbacks */
265	return platform_driver_register(&hvc_opal_driver);
266}
267device_initcall(hvc_opal_init);
268
269static void udbg_opal_putc(char c)
270{
271	unsigned int termno = hvc_opal_boot_termno;
272	int count = -1;
273
274	if (c == '\n')
275		udbg_opal_putc('\r');
276
277	do {
278		switch(hvc_opal_boot_priv.proto) {
279		case HV_PROTOCOL_RAW:
280			count = opal_put_chars(termno, &c, 1);
281			break;
282		case HV_PROTOCOL_HVSI:
283			count = hvc_opal_hvsi_put_chars(termno, &c, 1);
284			break;
285		}
286
287		/* This is needed for the cosole to flush
288		 * when there aren't any interrupts.
289		 */
290		opal_flush_console(termno);
291	} while(count == 0 || count == -EAGAIN);
292}
293
294static int udbg_opal_getc_poll(void)
295{
296	unsigned int termno = hvc_opal_boot_termno;
297	int rc = 0;
298	char c;
299
300	switch(hvc_opal_boot_priv.proto) {
301	case HV_PROTOCOL_RAW:
302		rc = opal_get_chars(termno, &c, 1);
303		break;
304	case HV_PROTOCOL_HVSI:
305		rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
306		break;
307	}
308	if (!rc)
309		return -1;
310	return c;
311}
312
313static int udbg_opal_getc(void)
314{
315	int ch;
316	for (;;) {
317		ch = udbg_opal_getc_poll();
318		if (ch != -1)
 
 
 
 
 
319			return ch;
 
320	}
321}
322
323static void udbg_init_opal_common(void)
324{
325	udbg_putc = udbg_opal_putc;
326	udbg_getc = udbg_opal_getc;
327	udbg_getc_poll = udbg_opal_getc_poll;
 
328}
329
330void __init hvc_opal_init_early(void)
331{
332	struct device_node *stdout_node = of_node_get(of_stdout);
333	const __be32 *termno;
334	const struct hv_ops *ops;
335	u32 index;
336
337	/* If the console wasn't in /chosen, try /ibm,opal */
338	if (!stdout_node) {
339		struct device_node *opal, *np;
340
341		/* Current OPAL takeover doesn't provide the stdout
342		 * path, so we hard wire it
343		 */
344		opal = of_find_node_by_path("/ibm,opal/consoles");
345		if (opal)
346			pr_devel("hvc_opal: Found consoles in new location\n");
347		if (!opal) {
348			opal = of_find_node_by_path("/ibm,opal");
349			if (opal)
350				pr_devel("hvc_opal: "
351					 "Found consoles in old location\n");
352		}
353		if (!opal)
354			return;
355		for_each_child_of_node(opal, np) {
356			if (of_node_name_eq(np, "serial")) {
357				stdout_node = np;
358				break;
359			}
360		}
361		of_node_put(opal);
362	}
363	if (!stdout_node)
364		return;
365	termno = of_get_property(stdout_node, "reg", NULL);
366	index = termno ? be32_to_cpup(termno) : 0;
367	if (index >= MAX_NR_HVC_CONSOLES)
368		return;
369	hvc_opal_privs[index] = &hvc_opal_boot_priv;
370
371	/* Check the protocol */
372	if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
373		hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
374		ops = &hvc_opal_raw_ops;
375		pr_devel("hvc_opal: Found RAW console\n");
376	}
377	else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
378		hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
379		ops = &hvc_opal_hvsi_ops;
380		hvsilib_init(&hvc_opal_boot_priv.hvsi,
381			     opal_get_chars, opal_put_chars_atomic,
382			     index, 1);
383		/* HVSI, perform the handshake now */
384		hvsilib_establish(&hvc_opal_boot_priv.hvsi);
385		pr_devel("hvc_opal: Found HVSI console\n");
386	} else
387		goto out;
388	hvc_opal_boot_termno = index;
389	udbg_init_opal_common();
390	add_preferred_console("hvc", index, NULL);
391	hvc_instantiate(index, index, ops);
392out:
393	of_node_put(stdout_node);
394}
395
396#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
397void __init udbg_init_debug_opal_raw(void)
398{
399	u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
400	hvc_opal_privs[index] = &hvc_opal_boot_priv;
401	hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
402	hvc_opal_boot_termno = index;
403	udbg_init_opal_common();
404}
405#endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
406
407#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
408void __init udbg_init_debug_opal_hvsi(void)
409{
410	u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
411	hvc_opal_privs[index] = &hvc_opal_boot_priv;
412	hvc_opal_boot_termno = index;
413	udbg_init_opal_common();
414	hvsilib_init(&hvc_opal_boot_priv.hvsi,
415		     opal_get_chars, opal_put_chars_atomic,
416		     index, 1);
417	hvsilib_establish(&hvc_opal_boot_priv.hvsi);
418}
419#endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */