fdt_serial.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #include <libfdt.h>
  10. #include <sbi/sbi_scratch.h>
  11. #include <sbi_utils/fdt/fdt_helper.h>
  12. #include <sbi_utils/serial/fdt_serial.h>
  13. extern struct fdt_serial fdt_serial_uart8250;
  14. extern struct fdt_serial fdt_serial_sifive;
  15. extern struct fdt_serial fdt_serial_htif;
  16. extern struct fdt_serial fdt_serial_shakti;
  17. static struct fdt_serial *serial_drivers[] = {
  18. &fdt_serial_uart8250,
  19. &fdt_serial_sifive,
  20. &fdt_serial_htif,
  21. &fdt_serial_shakti,
  22. };
  23. static struct fdt_serial dummy = {
  24. .match_table = NULL,
  25. .init = NULL,
  26. };
  27. static struct fdt_serial *current_driver = &dummy;
  28. int fdt_serial_init(void)
  29. {
  30. const void *prop;
  31. struct fdt_serial *drv;
  32. const struct fdt_match *match;
  33. int pos, noff = -1, len, coff, rc;
  34. void *fdt = sbi_scratch_thishart_arg1_ptr();
  35. /* Find offset of node pointed by stdout-path */
  36. coff = fdt_path_offset(fdt, "/chosen");
  37. if (-1 < coff) {
  38. prop = fdt_getprop(fdt, coff, "stdout-path", &len);
  39. if (prop && len)
  40. noff = fdt_path_offset(fdt, prop);
  41. }
  42. /* First check DT node pointed by stdout-path */
  43. for (pos = 0; pos < array_size(serial_drivers) && -1 < noff; pos++) {
  44. drv = serial_drivers[pos];
  45. match = fdt_match_node(fdt, noff, drv->match_table);
  46. if (!match)
  47. continue;
  48. if (drv->init) {
  49. rc = drv->init(fdt, noff, match);
  50. if (rc)
  51. return rc;
  52. }
  53. current_driver = drv;
  54. break;
  55. }
  56. /* Check if we found desired driver */
  57. if (current_driver != &dummy)
  58. goto done;
  59. /* Lastly check all DT nodes */
  60. for (pos = 0; pos < array_size(serial_drivers); pos++) {
  61. drv = serial_drivers[pos];
  62. noff = fdt_find_match(fdt, -1, drv->match_table, &match);
  63. if (noff < 0)
  64. continue;
  65. if (drv->init) {
  66. rc = drv->init(fdt, noff, match);
  67. if (rc)
  68. return rc;
  69. }
  70. current_driver = drv;
  71. break;
  72. }
  73. done:
  74. return 0;
  75. }