Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 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> |
Michael Neuling | 0084e47 | 2011-11-07 17:12:28 +1100 | [diff] [blame] | 31 | #include <linux/export.h> |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 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 | |
Bill Pemberton | de88b34 | 2012-11-19 13:24:32 -0500 | [diff] [blame] | 44 | static struct of_device_id hvc_opal_match[] = { |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 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; |
Benjamin Herrenschmidt | 32b941b | 2014-03-28 13:36:32 +1100 | [diff] [blame] | 64 | static bool hvc_opal_event_registered; |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 65 | |
| 66 | static 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 | |
| 74 | static 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 | |
| 84 | static 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 | |
| 94 | static 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 | |
| 108 | static 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 | |
| 119 | void 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 | |
| 130 | static 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 | |
| 139 | static 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 | |
| 155 | static 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 | |
Benjamin Herrenschmidt | 32b941b | 2014-03-28 13:36:32 +1100 | [diff] [blame] | 165 | static int hvc_opal_console_event(struct notifier_block *nb, |
| 166 | unsigned long events, void *change) |
| 167 | { |
| 168 | if (events & OPAL_EVENT_CONSOLE_INPUT) |
| 169 | hvc_kick(); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static struct notifier_block hvc_opal_console_nb = { |
| 174 | .notifier_call = hvc_opal_console_event, |
| 175 | }; |
| 176 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 177 | static int hvc_opal_probe(struct platform_device *dev) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 178 | { |
| 179 | const struct hv_ops *ops; |
| 180 | struct hvc_struct *hp; |
| 181 | struct hvc_opal_priv *pv; |
| 182 | hv_protocol_t proto; |
| 183 | unsigned int termno, boot = 0; |
| 184 | const __be32 *reg; |
| 185 | |
Benjamin Herrenschmidt | 32b941b | 2014-03-28 13:36:32 +1100 | [diff] [blame] | 186 | |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 187 | if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) { |
| 188 | proto = HV_PROTOCOL_RAW; |
| 189 | ops = &hvc_opal_raw_ops; |
| 190 | } else if (of_device_is_compatible(dev->dev.of_node, |
| 191 | "ibm,opal-console-hvsi")) { |
| 192 | proto = HV_PROTOCOL_HVSI; |
| 193 | ops = &hvc_opal_hvsi_ops; |
| 194 | } else { |
Masanari Iida | 46e99c4 | 2012-10-24 23:29:41 +0900 | [diff] [blame] | 195 | pr_err("hvc_opal: Unknown protocol for %s\n", |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 196 | dev->dev.of_node->full_name); |
| 197 | return -ENXIO; |
| 198 | } |
| 199 | |
| 200 | reg = of_get_property(dev->dev.of_node, "reg", NULL); |
| 201 | termno = reg ? be32_to_cpup(reg) : 0; |
| 202 | |
| 203 | /* Is it our boot one ? */ |
| 204 | if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) { |
| 205 | pv = hvc_opal_privs[termno]; |
| 206 | boot = 1; |
| 207 | } else if (hvc_opal_privs[termno] == NULL) { |
| 208 | pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL); |
| 209 | if (!pv) |
| 210 | return -ENOMEM; |
| 211 | pv->proto = proto; |
| 212 | hvc_opal_privs[termno] = pv; |
| 213 | if (proto == HV_PROTOCOL_HVSI) |
| 214 | hvsilib_init(&pv->hvsi, opal_get_chars, opal_put_chars, |
| 215 | termno, 0); |
| 216 | |
| 217 | /* Instanciate now to establish a mapping index==vtermno */ |
| 218 | hvc_instantiate(termno, termno, ops); |
| 219 | } else { |
| 220 | pr_err("hvc_opal: Device %s has duplicate terminal number #%d\n", |
| 221 | dev->dev.of_node->full_name, termno); |
| 222 | return -ENXIO; |
| 223 | } |
| 224 | |
| 225 | pr_info("hvc%d: %s protocol on %s%s\n", termno, |
| 226 | proto == HV_PROTOCOL_RAW ? "raw" : "hvsi", |
| 227 | dev->dev.of_node->full_name, |
| 228 | boot ? " (boot console)" : ""); |
| 229 | |
Benjamin Herrenschmidt | 32b941b | 2014-03-28 13:36:32 +1100 | [diff] [blame] | 230 | /* We don't do IRQ ... */ |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 231 | hp = hvc_alloc(termno, 0, ops, MAX_VIO_PUT_CHARS); |
| 232 | if (IS_ERR(hp)) |
| 233 | return PTR_ERR(hp); |
| 234 | dev_set_drvdata(&dev->dev, hp); |
| 235 | |
Benjamin Herrenschmidt | 32b941b | 2014-03-28 13:36:32 +1100 | [diff] [blame] | 236 | /* ... but we use OPAL event to kick the console */ |
| 237 | if (!hvc_opal_event_registered) { |
| 238 | opal_notifier_register(&hvc_opal_console_nb); |
| 239 | hvc_opal_event_registered = true; |
| 240 | } |
| 241 | |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 245 | static int hvc_opal_remove(struct platform_device *dev) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 246 | { |
| 247 | struct hvc_struct *hp = dev_get_drvdata(&dev->dev); |
| 248 | int rc, termno; |
| 249 | |
| 250 | termno = hp->vtermno; |
| 251 | rc = hvc_remove(hp); |
| 252 | if (rc == 0) { |
| 253 | if (hvc_opal_privs[termno] != &hvc_opal_boot_priv) |
| 254 | kfree(hvc_opal_privs[termno]); |
| 255 | hvc_opal_privs[termno] = NULL; |
| 256 | } |
| 257 | return rc; |
| 258 | } |
| 259 | |
| 260 | static struct platform_driver hvc_opal_driver = { |
| 261 | .probe = hvc_opal_probe, |
Bill Pemberton | 91116cb | 2012-11-19 13:21:06 -0500 | [diff] [blame] | 262 | .remove = hvc_opal_remove, |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 263 | .driver = { |
| 264 | .name = hvc_opal_name, |
| 265 | .owner = THIS_MODULE, |
| 266 | .of_match_table = hvc_opal_match, |
| 267 | } |
| 268 | }; |
| 269 | |
| 270 | static int __init hvc_opal_init(void) |
| 271 | { |
| 272 | if (!firmware_has_feature(FW_FEATURE_OPAL)) |
| 273 | return -ENODEV; |
| 274 | |
| 275 | /* Register as a vio device to receive callbacks */ |
| 276 | return platform_driver_register(&hvc_opal_driver); |
| 277 | } |
Paul Gortmaker | 4fedd0b | 2014-01-15 16:35:43 -0500 | [diff] [blame] | 278 | device_initcall(hvc_opal_init); |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 279 | |
| 280 | static void udbg_opal_putc(char c) |
| 281 | { |
| 282 | unsigned int termno = hvc_opal_boot_termno; |
| 283 | int count = -1; |
| 284 | |
| 285 | if (c == '\n') |
| 286 | udbg_opal_putc('\r'); |
| 287 | |
| 288 | do { |
| 289 | switch(hvc_opal_boot_priv.proto) { |
| 290 | case HV_PROTOCOL_RAW: |
| 291 | count = opal_put_chars(termno, &c, 1); |
| 292 | break; |
| 293 | case HV_PROTOCOL_HVSI: |
| 294 | count = hvc_opal_hvsi_put_chars(termno, &c, 1); |
| 295 | break; |
| 296 | } |
| 297 | } while(count == 0 || count == -EAGAIN); |
| 298 | } |
| 299 | |
| 300 | static int udbg_opal_getc_poll(void) |
| 301 | { |
| 302 | unsigned int termno = hvc_opal_boot_termno; |
| 303 | int rc = 0; |
| 304 | char c; |
| 305 | |
| 306 | switch(hvc_opal_boot_priv.proto) { |
| 307 | case HV_PROTOCOL_RAW: |
| 308 | rc = opal_get_chars(termno, &c, 1); |
| 309 | break; |
| 310 | case HV_PROTOCOL_HVSI: |
| 311 | rc = hvc_opal_hvsi_get_chars(termno, &c, 1); |
| 312 | break; |
| 313 | } |
| 314 | if (!rc) |
| 315 | return -1; |
| 316 | return c; |
| 317 | } |
| 318 | |
| 319 | static int udbg_opal_getc(void) |
| 320 | { |
| 321 | int ch; |
| 322 | for (;;) { |
| 323 | ch = udbg_opal_getc_poll(); |
| 324 | if (ch == -1) { |
| 325 | /* This shouldn't be needed...but... */ |
| 326 | volatile unsigned long delay; |
| 327 | for (delay=0; delay < 2000000; delay++) |
| 328 | ; |
| 329 | } else { |
| 330 | return ch; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static void udbg_init_opal_common(void) |
| 336 | { |
| 337 | udbg_putc = udbg_opal_putc; |
| 338 | udbg_getc = udbg_opal_getc; |
| 339 | udbg_getc_poll = udbg_opal_getc_poll; |
| 340 | tb_ticks_per_usec = 0x200; /* Make udelay not suck */ |
| 341 | } |
| 342 | |
| 343 | void __init hvc_opal_init_early(void) |
| 344 | { |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 345 | struct device_node *stdout_node = of_node_get(of_stdout); |
Benjamin Herrenschmidt | 5c94913 | 2013-09-23 12:05:08 +1000 | [diff] [blame] | 346 | const __be32 *termno; |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 347 | const struct hv_ops *ops; |
| 348 | u32 index; |
| 349 | |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 350 | /* If the console wasn't in /chosen, try /ibm,opal */ |
| 351 | if (!stdout_node) { |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 352 | struct device_node *opal, *np; |
| 353 | |
| 354 | /* Current OPAL takeover doesn't provide the stdout |
| 355 | * path, so we hard wire it |
| 356 | */ |
| 357 | opal = of_find_node_by_path("/ibm,opal/consoles"); |
| 358 | if (opal) |
| 359 | pr_devel("hvc_opal: Found consoles in new location\n"); |
| 360 | if (!opal) { |
| 361 | opal = of_find_node_by_path("/ibm,opal"); |
| 362 | if (opal) |
| 363 | pr_devel("hvc_opal: " |
| 364 | "Found consoles in old location\n"); |
| 365 | } |
| 366 | if (!opal) |
| 367 | return; |
| 368 | for_each_child_of_node(opal, np) { |
| 369 | if (!strcmp(np->name, "serial")) { |
| 370 | stdout_node = np; |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | of_node_put(opal); |
| 375 | } |
| 376 | if (!stdout_node) |
| 377 | return; |
| 378 | termno = of_get_property(stdout_node, "reg", NULL); |
Benjamin Herrenschmidt | 5c94913 | 2013-09-23 12:05:08 +1000 | [diff] [blame] | 379 | index = termno ? be32_to_cpup(termno) : 0; |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 380 | if (index >= MAX_NR_HVC_CONSOLES) |
| 381 | return; |
| 382 | hvc_opal_privs[index] = &hvc_opal_boot_priv; |
| 383 | |
| 384 | /* Check the protocol */ |
| 385 | if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) { |
| 386 | hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW; |
| 387 | ops = &hvc_opal_raw_ops; |
| 388 | pr_devel("hvc_opal: Found RAW console\n"); |
| 389 | } |
| 390 | else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) { |
| 391 | hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI; |
| 392 | ops = &hvc_opal_hvsi_ops; |
| 393 | hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars, |
| 394 | opal_put_chars, index, 1); |
| 395 | /* HVSI, perform the handshake now */ |
| 396 | hvsilib_establish(&hvc_opal_boot_priv.hvsi); |
| 397 | pr_devel("hvc_opal: Found HVSI console\n"); |
| 398 | } else |
| 399 | goto out; |
| 400 | hvc_opal_boot_termno = index; |
| 401 | udbg_init_opal_common(); |
| 402 | add_preferred_console("hvc", index, NULL); |
| 403 | hvc_instantiate(index, index, ops); |
| 404 | out: |
| 405 | of_node_put(stdout_node); |
| 406 | } |
| 407 | |
| 408 | #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW |
Benjamin Herrenschmidt | 50fb31c | 2012-07-04 15:51:10 +1000 | [diff] [blame] | 409 | void __init udbg_init_debug_opal_raw(void) |
Benjamin Herrenschmidt | daea117 | 2011-09-19 17:44:59 +0000 | [diff] [blame] | 410 | { |
| 411 | u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO; |
| 412 | hvc_opal_privs[index] = &hvc_opal_boot_priv; |
| 413 | hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW; |
| 414 | hvc_opal_boot_termno = index; |
| 415 | udbg_init_opal_common(); |
| 416 | } |
| 417 | #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */ |
| 418 | |
| 419 | #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI |
| 420 | void __init udbg_init_debug_opal_hvsi(void) |
| 421 | { |
| 422 | u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO; |
| 423 | hvc_opal_privs[index] = &hvc_opal_boot_priv; |
| 424 | hvc_opal_boot_termno = index; |
| 425 | udbg_init_opal_common(); |
| 426 | hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars, opal_put_chars, |
| 427 | index, 1); |
| 428 | hvsilib_establish(&hvc_opal_boot_priv.hvsi); |
| 429 | } |
| 430 | #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */ |