stdio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
  3. *
  4. * Changes for multibus/multiadapter I2C support.
  5. *
  6. * (C) Copyright 2000
  7. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <stdarg.h>
  16. #include <malloc.h>
  17. #include <stdio_dev.h>
  18. #include <serial.h>
  19. #ifdef CONFIG_LOGBUFFER
  20. #include <logbuff.h>
  21. #endif
  22. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  23. #include <i2c.h>
  24. #endif
  25. #include <dm/device-internal.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. static struct stdio_dev devs;
  28. struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
  29. char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
  30. #if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
  31. #define CONFIG_SYS_DEVICE_NULLDEV 1
  32. #endif
  33. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  34. #define CONFIG_SYS_DEVICE_NULLDEV 1
  35. #endif
  36. #ifdef CONFIG_SYS_DEVICE_NULLDEV
  37. static void nulldev_putc(struct stdio_dev *dev, const char c)
  38. {
  39. /* nulldev is empty! */
  40. }
  41. static void nulldev_puts(struct stdio_dev *dev, const char *s)
  42. {
  43. /* nulldev is empty! */
  44. }
  45. static int nulldev_input(struct stdio_dev *dev)
  46. {
  47. /* nulldev is empty! */
  48. return 0;
  49. }
  50. #endif
  51. static void stdio_serial_putc(struct stdio_dev *dev, const char c)
  52. {
  53. serial_putc(c);
  54. }
  55. static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
  56. {
  57. serial_puts(s);
  58. }
  59. static int stdio_serial_getc(struct stdio_dev *dev)
  60. {
  61. return serial_getc();
  62. }
  63. static int stdio_serial_tstc(struct stdio_dev *dev)
  64. {
  65. return serial_tstc();
  66. }
  67. /**************************************************************************
  68. * SYSTEM DRIVERS
  69. **************************************************************************
  70. */
  71. static void drv_system_init (void)
  72. {
  73. struct stdio_dev dev;
  74. memset (&dev, 0, sizeof (dev));
  75. strcpy (dev.name, "serial");
  76. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  77. dev.putc = stdio_serial_putc;
  78. dev.puts = stdio_serial_puts;
  79. dev.getc = stdio_serial_getc;
  80. dev.tstc = stdio_serial_tstc;
  81. stdio_register (&dev);
  82. #ifdef CONFIG_SYS_DEVICE_NULLDEV
  83. memset (&dev, 0, sizeof (dev));
  84. strcpy (dev.name, "nulldev");
  85. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  86. dev.putc = nulldev_putc;
  87. dev.puts = nulldev_puts;
  88. dev.getc = nulldev_input;
  89. dev.tstc = nulldev_input;
  90. stdio_register (&dev);
  91. #endif
  92. }
  93. /**************************************************************************
  94. * DEVICES
  95. **************************************************************************
  96. */
  97. struct list_head* stdio_get_list(void)
  98. {
  99. return &(devs.list);
  100. }
  101. struct stdio_dev* stdio_get_by_name(const char *name)
  102. {
  103. struct list_head *pos;
  104. struct stdio_dev *dev;
  105. if(!name)
  106. return NULL;
  107. list_for_each(pos, &(devs.list)) {
  108. dev = list_entry(pos, struct stdio_dev, list);
  109. if(strcmp(dev->name, name) == 0)
  110. return dev;
  111. }
  112. return NULL;
  113. }
  114. struct stdio_dev* stdio_clone(struct stdio_dev *dev)
  115. {
  116. struct stdio_dev *_dev;
  117. if(!dev)
  118. return NULL;
  119. _dev = calloc(1, sizeof(struct stdio_dev));
  120. if(!_dev)
  121. return NULL;
  122. memcpy(_dev, dev, sizeof(struct stdio_dev));
  123. return _dev;
  124. }
  125. int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
  126. {
  127. struct stdio_dev *_dev;
  128. _dev = stdio_clone(dev);
  129. if(!_dev)
  130. return -ENODEV;
  131. list_add_tail(&(_dev->list), &(devs.list));
  132. if (devp)
  133. *devp = _dev;
  134. return 0;
  135. }
  136. int stdio_register(struct stdio_dev *dev)
  137. {
  138. return stdio_register_dev(dev, NULL);
  139. }
  140. /* deregister the device "devname".
  141. * returns 0 if success, -1 if device is assigned and 1 if devname not found
  142. */
  143. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  144. int stdio_deregister_dev(struct stdio_dev *dev, int force)
  145. {
  146. int l;
  147. struct list_head *pos;
  148. char temp_names[3][16];
  149. /* get stdio devices (ListRemoveItem changes the dev list) */
  150. for (l=0 ; l< MAX_FILES; l++) {
  151. if (stdio_devices[l] == dev) {
  152. if (force) {
  153. strcpy(temp_names[l], "nulldev");
  154. continue;
  155. }
  156. /* Device is assigned -> report error */
  157. return -1;
  158. }
  159. memcpy (&temp_names[l][0],
  160. stdio_devices[l]->name,
  161. sizeof(temp_names[l]));
  162. }
  163. list_del(&(dev->list));
  164. free(dev);
  165. /* reassign Device list */
  166. list_for_each(pos, &(devs.list)) {
  167. dev = list_entry(pos, struct stdio_dev, list);
  168. for (l=0 ; l< MAX_FILES; l++) {
  169. if(strcmp(dev->name, temp_names[l]) == 0)
  170. stdio_devices[l] = dev;
  171. }
  172. }
  173. return 0;
  174. }
  175. int stdio_deregister(const char *devname, int force)
  176. {
  177. struct stdio_dev *dev;
  178. dev = stdio_get_by_name(devname);
  179. if (!dev) /* device not found */
  180. return -ENODEV;
  181. return stdio_deregister_dev(dev, force);
  182. }
  183. #endif /* CONFIG_SYS_STDIO_DEREGISTER */
  184. int stdio_init_tables(void)
  185. {
  186. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  187. /* already relocated for current ARM implementation */
  188. ulong relocation_offset = gd->reloc_off;
  189. int i;
  190. /* relocate device name pointers */
  191. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  192. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  193. relocation_offset);
  194. }
  195. #endif /* CONFIG_NEEDS_MANUAL_RELOC */
  196. /* Initialize the list */
  197. INIT_LIST_HEAD(&(devs.list));
  198. return 0;
  199. }
  200. int stdio_add_devices(void)
  201. {
  202. #ifdef CONFIG_DM_KEYBOARD
  203. struct udevice *dev;
  204. struct uclass *uc;
  205. int ret;
  206. /*
  207. * For now we probe all the devices here. At some point this should be
  208. * done only when the devices are required - e.g. we have a list of
  209. * input devices to start up in the stdin environment variable. That
  210. * work probably makes more sense when stdio itself is converted to
  211. * driver model.
  212. *
  213. * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc.
  214. * to return the device even on error. Then we could use that here.
  215. */
  216. ret = uclass_get(UCLASS_KEYBOARD, &uc);
  217. if (ret)
  218. return ret;
  219. /* Don't report errors to the caller - assume that they are non-fatal */
  220. uclass_foreach_dev(dev, uc) {
  221. ret = device_probe(dev);
  222. if (ret)
  223. printf("Failed to probe keyboard '%s'\n", dev->name);
  224. }
  225. #endif
  226. #ifdef CONFIG_SYS_I2C
  227. i2c_init_all();
  228. #else
  229. #if defined(CONFIG_HARD_I2C)
  230. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  231. #endif
  232. #endif
  233. #ifdef CONFIG_DM_VIDEO
  234. struct udevice *vdev;
  235. for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
  236. vdev;
  237. ret = uclass_next_device(&vdev))
  238. ;
  239. if (ret)
  240. printf("%s: Video device failed (ret=%d)\n", __func__, ret);
  241. #else
  242. # if defined(CONFIG_LCD)
  243. drv_lcd_init ();
  244. # endif
  245. # if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
  246. drv_video_init ();
  247. # endif
  248. #endif /* CONFIG_DM_VIDEO */
  249. #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
  250. drv_keyboard_init ();
  251. #endif
  252. #ifdef CONFIG_LOGBUFFER
  253. drv_logbuff_init ();
  254. #endif
  255. drv_system_init ();
  256. serial_stdio_init ();
  257. #ifdef CONFIG_USB_TTY
  258. drv_usbtty_init ();
  259. #endif
  260. #ifdef CONFIG_NETCONSOLE
  261. drv_nc_init ();
  262. #endif
  263. #ifdef CONFIG_JTAG_CONSOLE
  264. drv_jtag_console_init ();
  265. #endif
  266. #ifdef CONFIG_CBMEM_CONSOLE
  267. cbmemc_init();
  268. #endif
  269. return 0;
  270. }
  271. int stdio_init(void)
  272. {
  273. stdio_init_tables();
  274. stdio_add_devices();
  275. return 0;
  276. }