hvc_dcc.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2010, 2014 The Linux Foundation. All rights reserved. */
  3. #include <linux/init.h>
  4. #include <asm/dcc.h>
  5. #include <asm/processor.h>
  6. #include "hvc_console.h"
  7. /* DCC Status Bits */
  8. #define DCC_STATUS_RX (1 << 30)
  9. #define DCC_STATUS_TX (1 << 29)
  10. static int hvc_dcc_put_chars(uint32_t vt, const char *buf, int count)
  11. {
  12. int i;
  13. for (i = 0; i < count; i++) {
  14. while (__dcc_getstatus() & DCC_STATUS_TX)
  15. cpu_relax();
  16. __dcc_putchar(buf[i]);
  17. }
  18. return count;
  19. }
  20. static int hvc_dcc_get_chars(uint32_t vt, char *buf, int count)
  21. {
  22. int i;
  23. for (i = 0; i < count; ++i)
  24. if (__dcc_getstatus() & DCC_STATUS_RX)
  25. buf[i] = __dcc_getchar();
  26. else
  27. break;
  28. return i;
  29. }
  30. static bool hvc_dcc_check(void)
  31. {
  32. unsigned long time = jiffies + (HZ / 10);
  33. /* Write a test character to check if it is handled */
  34. __dcc_putchar('\n');
  35. while (time_is_after_jiffies(time)) {
  36. if (!(__dcc_getstatus() & DCC_STATUS_TX))
  37. return true;
  38. }
  39. return false;
  40. }
  41. static const struct hv_ops hvc_dcc_get_put_ops = {
  42. .get_chars = hvc_dcc_get_chars,
  43. .put_chars = hvc_dcc_put_chars,
  44. };
  45. static int __init hvc_dcc_console_init(void)
  46. {
  47. int ret;
  48. if (!hvc_dcc_check())
  49. return -ENODEV;
  50. /* Returns -1 if error */
  51. ret = hvc_instantiate(0, 0, &hvc_dcc_get_put_ops);
  52. return ret < 0 ? -ENODEV : 0;
  53. }
  54. console_initcall(hvc_dcc_console_init);
  55. static int __init hvc_dcc_init(void)
  56. {
  57. struct hvc_struct *p;
  58. if (!hvc_dcc_check())
  59. return -ENODEV;
  60. p = hvc_alloc(0, 0, &hvc_dcc_get_put_ops, 128);
  61. return PTR_ERR_OR_ZERO(p);
  62. }
  63. device_initcall(hvc_dcc_init);