serial-uclass.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. static void serial_putc_dev(struct udevice *dev, 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_putc(char ch)
  74. {
  75. serial_putc_dev(cur_dev, ch);
  76. }
  77. void serial_setbrg(void)
  78. {
  79. struct dm_serial_ops *ops = serial_get_ops(cur_dev);
  80. if (ops->setbrg)
  81. ops->setbrg(cur_dev, gd->baudrate);
  82. }
  83. void serial_puts(const char *str)
  84. {
  85. while (*str)
  86. serial_putc(*str++);
  87. }
  88. int serial_tstc(void)
  89. {
  90. struct dm_serial_ops *ops = serial_get_ops(cur_dev);
  91. if (ops->pending)
  92. return ops->pending(cur_dev, true);
  93. return 1;
  94. }
  95. static int serial_getc_dev(struct udevice *dev)
  96. {
  97. struct dm_serial_ops *ops = serial_get_ops(dev);
  98. int err;
  99. do {
  100. err = ops->getc(dev);
  101. } while (err == -EAGAIN);
  102. return err >= 0 ? err : 0;
  103. }
  104. int serial_getc(void)
  105. {
  106. return serial_getc_dev(cur_dev);
  107. }
  108. void serial_stdio_init(void)
  109. {
  110. }
  111. static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
  112. {
  113. struct udevice *dev = sdev->priv;
  114. serial_putc_dev(dev, ch);
  115. }
  116. void serial_stub_puts(struct stdio_dev *sdev, const char *str)
  117. {
  118. while (*str)
  119. serial_stub_putc(sdev, *str++);
  120. }
  121. int serial_stub_getc(struct stdio_dev *sdev)
  122. {
  123. struct udevice *dev = sdev->priv;
  124. return serial_getc_dev(dev);
  125. }
  126. int serial_stub_tstc(struct stdio_dev *sdev)
  127. {
  128. struct udevice *dev = sdev->priv;
  129. struct dm_serial_ops *ops = serial_get_ops(dev);
  130. if (ops->pending)
  131. return ops->pending(dev, true);
  132. return 1;
  133. }
  134. static int serial_post_probe(struct udevice *dev)
  135. {
  136. struct stdio_dev sdev;
  137. struct dm_serial_ops *ops = serial_get_ops(dev);
  138. struct serial_dev_priv *upriv = dev->uclass_priv;
  139. int ret;
  140. /* Set the baud rate */
  141. if (ops->setbrg) {
  142. ret = ops->setbrg(dev, gd->baudrate);
  143. if (ret)
  144. return ret;
  145. }
  146. if (!(gd->flags & GD_FLG_RELOC))
  147. return 0;
  148. memset(&sdev, '\0', sizeof(sdev));
  149. strncpy(sdev.name, dev->name, sizeof(sdev.name));
  150. sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  151. sdev.priv = dev;
  152. sdev.putc = serial_stub_putc;
  153. sdev.puts = serial_stub_puts;
  154. sdev.getc = serial_stub_getc;
  155. sdev.tstc = serial_stub_tstc;
  156. stdio_register_dev(&sdev, &upriv->sdev);
  157. return 0;
  158. }
  159. static int serial_pre_remove(struct udevice *dev)
  160. {
  161. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  162. struct serial_dev_priv *upriv = dev->uclass_priv;
  163. if (stdio_deregister_dev(upriv->sdev, 0))
  164. return -EPERM;
  165. #endif
  166. return 0;
  167. }
  168. UCLASS_DRIVER(serial) = {
  169. .id = UCLASS_SERIAL,
  170. .name = "serial",
  171. .post_probe = serial_post_probe,
  172. .pre_remove = serial_pre_remove,
  173. .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
  174. };