serial-uclass.c 4.2 KB

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