opal.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2016 IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include "ops.h"
  10. #include "stdio.h"
  11. #include "io.h"
  12. #include <libfdt.h>
  13. #include "../include/asm/opal-api.h"
  14. /* Global OPAL struct used by opal-call.S */
  15. struct opal {
  16. u64 base;
  17. u64 entry;
  18. } opal;
  19. static u32 opal_con_id;
  20. /* see opal-wrappers.S */
  21. int64_t opal_console_write(int64_t term_number, u64 *length, const u8 *buffer);
  22. int64_t opal_console_read(int64_t term_number, uint64_t *length, u8 *buffer);
  23. int64_t opal_console_write_buffer_space(uint64_t term_number, uint64_t *length);
  24. int64_t opal_console_flush(uint64_t term_number);
  25. int64_t opal_poll_events(uint64_t *outstanding_event_mask);
  26. void opal_kentry(unsigned long fdt_addr, void *vmlinux_addr);
  27. static int opal_con_open(void)
  28. {
  29. /*
  30. * When OPAL loads the boot kernel it stashes the OPAL base and entry
  31. * address in r8 and r9 so the kernel can use the OPAL console
  32. * before unflattening the devicetree. While executing the wrapper will
  33. * probably trash r8 and r9 so this kentry hook restores them before
  34. * entering the decompressed kernel.
  35. */
  36. platform_ops.kentry = opal_kentry;
  37. return 0;
  38. }
  39. static void opal_con_putc(unsigned char c)
  40. {
  41. int64_t rc;
  42. uint64_t olen, len;
  43. do {
  44. rc = opal_console_write_buffer_space(opal_con_id, &olen);
  45. len = be64_to_cpu(olen);
  46. if (rc)
  47. return;
  48. opal_poll_events(NULL);
  49. } while (len < 1);
  50. olen = cpu_to_be64(1);
  51. opal_console_write(opal_con_id, &olen, &c);
  52. }
  53. static void opal_con_close(void)
  54. {
  55. opal_console_flush(opal_con_id);
  56. }
  57. static void opal_init(void)
  58. {
  59. void *opal_node;
  60. opal_node = finddevice("/ibm,opal");
  61. if (!opal_node)
  62. return;
  63. if (getprop(opal_node, "opal-base-address", &opal.base, sizeof(u64)) < 0)
  64. return;
  65. opal.base = be64_to_cpu(opal.base);
  66. if (getprop(opal_node, "opal-entry-address", &opal.entry, sizeof(u64)) < 0)
  67. return;
  68. opal.entry = be64_to_cpu(opal.entry);
  69. }
  70. int opal_console_init(void *devp, struct serial_console_data *scdp)
  71. {
  72. opal_init();
  73. if (devp) {
  74. int n = getprop(devp, "reg", &opal_con_id, sizeof(u32));
  75. if (n != sizeof(u32))
  76. return -1;
  77. opal_con_id = be32_to_cpu(opal_con_id);
  78. } else
  79. opal_con_id = 0;
  80. scdp->open = opal_con_open;
  81. scdp->putc = opal_con_putc;
  82. scdp->close = opal_con_close;
  83. return 0;
  84. }