linux/drivers/tty/hvc/hvc_opal.c
<<
>>
Prefs
   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 = irq_of_parse_and_map(dev->dev.of_node, 0);
 218        if (!irq) {
 219                pr_info("hvc%d: No interrupts property, using OPAL event\n",
 220                                termno);
 221                irq = opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT));
 222        }
 223
 224        if (!irq) {
 225                pr_err("hvc_opal: Unable to map interrupt for device %s\n",
 226                        dev->dev.of_node->full_name);
 227                return irq;
 228        }
 229
 230        hp = hvc_alloc(termno, irq, ops, MAX_VIO_PUT_CHARS);
 231        if (IS_ERR(hp))
 232                return PTR_ERR(hp);
 233
 234        /* hvc consoles on powernv may need to share a single irq */
 235        hp->flags = IRQF_SHARED;
 236        dev_set_drvdata(&dev->dev, hp);
 237
 238        return 0;
 239}
 240
 241static int hvc_opal_remove(struct platform_device *dev)
 242{
 243        struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
 244        int rc, termno;
 245
 246        termno = hp->vtermno;
 247        rc = hvc_remove(hp);
 248        if (rc == 0) {
 249                if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
 250                        kfree(hvc_opal_privs[termno]);
 251                hvc_opal_privs[termno] = NULL;
 252        }
 253        return rc;
 254}
 255
 256static struct platform_driver hvc_opal_driver = {
 257        .probe          = hvc_opal_probe,
 258        .remove         = hvc_opal_remove,
 259        .driver         = {
 260                .name   = hvc_opal_name,
 261                .of_match_table = hvc_opal_match,
 262        }
 263};
 264
 265static int __init hvc_opal_init(void)
 266{
 267        if (!firmware_has_feature(FW_FEATURE_OPAL))
 268                return -ENODEV;
 269
 270        /* Register as a vio device to receive callbacks */
 271        return platform_driver_register(&hvc_opal_driver);
 272}
 273device_initcall(hvc_opal_init);
 274
 275static void udbg_opal_putc(char c)
 276{
 277        unsigned int termno = hvc_opal_boot_termno;
 278        int count = -1;
 279
 280        if (c == '\n')
 281                udbg_opal_putc('\r');
 282
 283        do {
 284                switch(hvc_opal_boot_priv.proto) {
 285                case HV_PROTOCOL_RAW:
 286                        count = opal_put_chars(termno, &c, 1);
 287                        break;
 288                case HV_PROTOCOL_HVSI:
 289                        count = hvc_opal_hvsi_put_chars(termno, &c, 1);
 290                        break;
 291                }
 292        } while(count == 0 || count == -EAGAIN);
 293}
 294
 295static int udbg_opal_getc_poll(void)
 296{
 297        unsigned int termno = hvc_opal_boot_termno;
 298        int rc = 0;
 299        char c;
 300
 301        switch(hvc_opal_boot_priv.proto) {
 302        case HV_PROTOCOL_RAW:
 303                rc = opal_get_chars(termno, &c, 1);
 304                break;
 305        case HV_PROTOCOL_HVSI:
 306                rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
 307                break;
 308        }
 309        if (!rc)
 310                return -1;
 311        return c;
 312}
 313
 314static int udbg_opal_getc(void)
 315{
 316        int ch;
 317        for (;;) {
 318                ch = udbg_opal_getc_poll();
 319                if (ch == -1) {
 320                        /* This shouldn't be needed...but... */
 321                        volatile unsigned long delay;
 322                        for (delay=0; delay < 2000000; delay++)
 323                                ;
 324                } else {
 325                        return ch;
 326                }
 327        }
 328}
 329
 330static void udbg_init_opal_common(void)
 331{
 332        udbg_putc = udbg_opal_putc;
 333        udbg_getc = udbg_opal_getc;
 334        udbg_getc_poll = udbg_opal_getc_poll;
 335        tb_ticks_per_usec = 0x200; /* Make udelay not suck */
 336}
 337
 338void __init hvc_opal_init_early(void)
 339{
 340        struct device_node *stdout_node = of_node_get(of_stdout);
 341        const __be32 *termno;
 342        const struct hv_ops *ops;
 343        u32 index;
 344
 345        /* If the console wasn't in /chosen, try /ibm,opal */
 346        if (!stdout_node) {
 347                struct device_node *opal, *np;
 348
 349                /* Current OPAL takeover doesn't provide the stdout
 350                 * path, so we hard wire it
 351                 */
 352                opal = of_find_node_by_path("/ibm,opal/consoles");
 353                if (opal)
 354                        pr_devel("hvc_opal: Found consoles in new location\n");
 355                if (!opal) {
 356                        opal = of_find_node_by_path("/ibm,opal");
 357                        if (opal)
 358                                pr_devel("hvc_opal: "
 359                                         "Found consoles in old location\n");
 360                }
 361                if (!opal)
 362                        return;
 363                for_each_child_of_node(opal, np) {
 364                        if (!strcmp(np->name, "serial")) {
 365                                stdout_node = np;
 366                                break;
 367                        }
 368                }
 369                of_node_put(opal);
 370        }
 371        if (!stdout_node)
 372                return;
 373        termno = of_get_property(stdout_node, "reg", NULL);
 374        index = termno ? be32_to_cpup(termno) : 0;
 375        if (index >= MAX_NR_HVC_CONSOLES)
 376                return;
 377        hvc_opal_privs[index] = &hvc_opal_boot_priv;
 378
 379        /* Check the protocol */
 380        if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
 381                hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
 382                ops = &hvc_opal_raw_ops;
 383                pr_devel("hvc_opal: Found RAW console\n");
 384        }
 385        else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
 386                hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
 387                ops = &hvc_opal_hvsi_ops;
 388                hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars,
 389                             opal_put_chars, index, 1);
 390                /* HVSI, perform the handshake now */
 391                hvsilib_establish(&hvc_opal_boot_priv.hvsi);
 392                pr_devel("hvc_opal: Found HVSI console\n");
 393        } else
 394                goto out;
 395        hvc_opal_boot_termno = index;
 396        udbg_init_opal_common();
 397        add_preferred_console("hvc", index, NULL);
 398        hvc_instantiate(index, index, ops);
 399out:
 400        of_node_put(stdout_node);
 401}
 402
 403#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
 404void __init udbg_init_debug_opal_raw(void)
 405{
 406        u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
 407        hvc_opal_privs[index] = &hvc_opal_boot_priv;
 408        hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
 409        hvc_opal_boot_termno = index;
 410        udbg_init_opal_common();
 411}
 412#endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
 413
 414#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
 415void __init udbg_init_debug_opal_hvsi(void)
 416{
 417        u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
 418        hvc_opal_privs[index] = &hvc_opal_boot_priv;
 419        hvc_opal_boot_termno = index;
 420        udbg_init_opal_common();
 421        hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars, opal_put_chars,
 422                     index, 1);
 423        hvsilib_establish(&hvc_opal_boot_priv.hvsi);
 424}
 425#endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */
 426