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