opal.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifdef __powerpc64__
  15. /* Global OPAL struct used by opal-call.S */
  16. struct opal {
  17. u64 base;
  18. u64 entry;
  19. } opal;
  20. static u32 opal_con_id;
  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. static int opal_con_open(void)
  27. {
  28. return 0;
  29. }
  30. static void opal_con_putc(unsigned char c)
  31. {
  32. int64_t rc;
  33. uint64_t olen, len;
  34. do {
  35. rc = opal_console_write_buffer_space(opal_con_id, &olen);
  36. len = be64_to_cpu(olen);
  37. if (rc)
  38. return;
  39. opal_poll_events(NULL);
  40. } while (len < 1);
  41. olen = cpu_to_be64(1);
  42. opal_console_write(opal_con_id, &olen, &c);
  43. }
  44. static void opal_con_close(void)
  45. {
  46. opal_console_flush(opal_con_id);
  47. }
  48. static void opal_init(void)
  49. {
  50. void *opal_node;
  51. opal_node = finddevice("/ibm,opal");
  52. if (!opal_node)
  53. return;
  54. if (getprop(opal_node, "opal-base-address", &opal.base, sizeof(u64)) < 0)
  55. return;
  56. opal.base = be64_to_cpu(opal.base);
  57. if (getprop(opal_node, "opal-entry-address", &opal.entry, sizeof(u64)) < 0)
  58. return;
  59. opal.entry = be64_to_cpu(opal.entry);
  60. }
  61. int opal_console_init(void *devp, struct serial_console_data *scdp)
  62. {
  63. opal_init();
  64. if (devp) {
  65. int n = getprop(devp, "reg", &opal_con_id, sizeof(u32));
  66. if (n != sizeof(u32))
  67. return -1;
  68. opal_con_id = be32_to_cpu(opal_con_id);
  69. } else
  70. opal_con_id = 0;
  71. scdp->open = opal_con_open;
  72. scdp->putc = opal_con_putc;
  73. scdp->close = opal_con_close;
  74. return 0;
  75. }
  76. #else
  77. int opal_console_init(void *devp, struct serial_console_data *scdp)
  78. {
  79. return -1;
  80. }
  81. #endif /* __powerpc64__ */