serial-uclass.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2014 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <os.h>
  11. #include <serial.h>
  12. #include <stdio_dev.h>
  13. #include <dm/lists.h>
  14. #include <dm/device-internal.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /* The currently-selected console serial device */
  17. struct udevice *cur_dev __attribute__ ((section(".data")));
  18. #ifndef CONFIG_SYS_MALLOC_F_LEN
  19. #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
  20. #endif
  21. static void serial_find_console_or_panic(void)
  22. {
  23. int node;
  24. /* Check for a chosen console */
  25. node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
  26. if (node < 0)
  27. node = fdtdec_get_alias_node(gd->fdt_blob, "console");
  28. if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev))
  29. return;
  30. /*
  31. * If the console is not marked to be bound before relocation, bind
  32. * it anyway.
  33. */
  34. if (node > 0 &&
  35. !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) {
  36. if (!device_probe(cur_dev))
  37. return;
  38. cur_dev = NULL;
  39. }
  40. /*
  41. * Failing that, get the device with sequence number 0, or in extremis
  42. * just the first serial device we can find. But we insist on having
  43. * a console (even if it is silent).
  44. */
  45. if (uclass_get_device_by_seq(UCLASS_SERIAL, 0, &cur_dev) &&
  46. (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev))
  47. panic("No serial driver found");
  48. }
  49. /* Called prior to relocation */
  50. int serial_init(void)
  51. {
  52. serial_find_console_or_panic();
  53. gd->flags |= GD_FLG_SERIAL_READY;
  54. return 0;
  55. }
  56. /* Called after relocation */
  57. void serial_initialize(void)
  58. {
  59. serial_find_console_or_panic();
  60. }
  61. void serial_putc(char ch)
  62. {
  63. struct dm_serial_ops *ops = serial_get_ops(cur_dev);
  64. int err;
  65. do {
  66. err = ops->putc(cur_dev, ch);
  67. } while (err == -EAGAIN);
  68. if (ch == '\n')
  69. serial_putc('\r');
  70. }
  71. void serial_setbrg(void)
  72. {
  73. struct dm_serial_ops *ops = serial_get_ops(cur_dev);
  74. if (ops->setbrg)
  75. ops->setbrg(cur_dev, gd->baudrate);
  76. }
  77. void serial_puts(const char *str)
  78. {
  79. while (*str)
  80. serial_putc(*str++);
  81. }
  82. int serial_tstc(void)
  83. {
  84. struct dm_serial_ops *ops = serial_get_ops(cur_dev);
  85. if (ops->pending)
  86. return ops->pending(cur_dev, true);
  87. return 1;
  88. }
  89. int serial_getc(void)
  90. {
  91. struct dm_serial_ops *ops = serial_get_ops(cur_dev);
  92. int err;
  93. do {
  94. err = ops->getc(cur_dev);
  95. } while (err == -EAGAIN);
  96. return err >= 0 ? err : 0;
  97. }
  98. void serial_stdio_init(void)
  99. {
  100. }
  101. void serial_stub_putc(struct stdio_dev *sdev, const char ch)
  102. {
  103. struct udevice *dev = sdev->priv;
  104. struct dm_serial_ops *ops = serial_get_ops(dev);
  105. ops->putc(dev, ch);
  106. }
  107. void serial_stub_puts(struct stdio_dev *sdev, const char *str)
  108. {
  109. while (*str)
  110. serial_stub_putc(sdev, *str++);
  111. }
  112. int serial_stub_getc(struct stdio_dev *sdev)
  113. {
  114. struct udevice *dev = sdev->priv;
  115. struct dm_serial_ops *ops = serial_get_ops(dev);
  116. int err;
  117. do {
  118. err = ops->getc(dev);
  119. } while (err == -EAGAIN);
  120. return err >= 0 ? err : 0;
  121. }
  122. int serial_stub_tstc(struct stdio_dev *sdev)
  123. {
  124. struct udevice *dev = sdev->priv;
  125. struct dm_serial_ops *ops = serial_get_ops(dev);
  126. if (ops->pending)
  127. return ops->pending(dev, true);
  128. return 1;
  129. }
  130. static int serial_post_probe(struct udevice *dev)
  131. {
  132. struct stdio_dev sdev;
  133. struct dm_serial_ops *ops = serial_get_ops(dev);
  134. struct serial_dev_priv *upriv = dev->uclass_priv;
  135. int ret;
  136. /* Set the baud rate */
  137. if (ops->setbrg) {
  138. ret = ops->setbrg(dev, gd->baudrate);
  139. if (ret)
  140. return ret;
  141. }
  142. if (!(gd->flags & GD_FLG_RELOC))
  143. return 0;
  144. memset(&sdev, '\0', sizeof(sdev));
  145. strncpy(sdev.name, dev->name, sizeof(sdev.name));
  146. sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  147. sdev.priv = dev;
  148. sdev.putc = serial_stub_putc;
  149. sdev.puts = serial_stub_puts;
  150. sdev.getc = serial_stub_getc;
  151. sdev.tstc = serial_stub_tstc;
  152. stdio_register_dev(&sdev, &upriv->sdev);
  153. return 0;
  154. }
  155. static int serial_pre_remove(struct udevice *dev)
  156. {
  157. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  158. struct serial_dev_priv *upriv = dev->uclass_priv;
  159. if (stdio_deregister_dev(upriv->sdev))
  160. return -EPERM;
  161. #endif
  162. return 0;
  163. }
  164. UCLASS_DRIVER(serial) = {
  165. .id = UCLASS_SERIAL,
  166. .name = "serial",
  167. .post_probe = serial_post_probe,
  168. .pre_remove = serial_pre_remove,
  169. .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
  170. };