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