fdt_serial.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_error.h>
  11. #include <sbi/sbi_scratch.h>
  12. #include <sbi_utils/fdt/fdt_helper.h>
  13. #include <sbi_utils/serial/fdt_serial.h>
  14. extern struct fdt_serial fdt_serial_uart8250;
  15. extern struct fdt_serial fdt_serial_sifive;
  16. extern struct fdt_serial fdt_serial_htif;
  17. extern struct fdt_serial fdt_serial_shakti;
  18. extern struct fdt_serial fdt_serial_gaisler;
  19. static struct fdt_serial *serial_drivers[] = {
  20. &fdt_serial_uart8250,
  21. &fdt_serial_sifive,
  22. &fdt_serial_htif,
  23. &fdt_serial_shakti,
  24. &fdt_serial_gaisler
  25. };
  26. static struct fdt_serial dummy = {
  27. .match_table = NULL,
  28. .init = NULL,
  29. };
  30. static struct fdt_serial *current_driver = &dummy;
  31. int fdt_serial_init(void)
  32. {
  33. const void *prop;
  34. struct fdt_serial *drv;
  35. const struct fdt_match *match;
  36. int pos, noff = -1, len, coff, rc;
  37. void *fdt = sbi_scratch_thishart_arg1_ptr();
  38. /* Find offset of node pointed to by stdout-path */
  39. coff = fdt_path_offset(fdt, "/chosen");
  40. if (-1 < coff) {
  41. prop = fdt_getprop(fdt, coff, "stdout-path", &len);
  42. if (prop && len) {
  43. const char *sep, *start = prop;
  44. /* The device path may be followed by ':' */
  45. sep = strchr(start, ':');
  46. if (sep)
  47. noff = fdt_path_offset_namelen(fdt, prop,
  48. sep - start);
  49. else
  50. noff = fdt_path_offset(fdt, prop);
  51. }
  52. }
  53. /* First check DT node pointed by stdout-path */
  54. for (pos = 0; pos < array_size(serial_drivers) && -1 < noff; pos++) {
  55. drv = serial_drivers[pos];
  56. match = fdt_match_node(fdt, noff, drv->match_table);
  57. if (!match)
  58. continue;
  59. if (drv->init) {
  60. rc = drv->init(fdt, noff, match);
  61. if (rc == SBI_ENODEV)
  62. continue;
  63. if (rc)
  64. return rc;
  65. }
  66. current_driver = drv;
  67. break;
  68. }
  69. /* Check if we found desired driver */
  70. if (current_driver != &dummy)
  71. goto done;
  72. /* Lastly check all DT nodes */
  73. for (pos = 0; pos < array_size(serial_drivers); pos++) {
  74. drv = serial_drivers[pos];
  75. noff = fdt_find_match(fdt, -1, drv->match_table, &match);
  76. if (noff < 0)
  77. continue;
  78. if (drv->init) {
  79. rc = drv->init(fdt, noff, match);
  80. if (rc == SBI_ENODEV)
  81. continue;
  82. if (rc)
  83. return rc;
  84. }
  85. current_driver = drv;
  86. break;
  87. }
  88. done:
  89. return 0;
  90. }