serial-uclass.c 4.3 KB

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