stdio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
  4. *
  5. * Changes for multibus/multiadapter I2C support.
  6. *
  7. * (C) Copyright 2000
  8. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  9. */
  10. #include <config.h>
  11. #include <common.h>
  12. #include <dm.h>
  13. #include <errno.h>
  14. #include <stdarg.h>
  15. #include <malloc.h>
  16. #include <stdio_dev.h>
  17. #include <serial.h>
  18. #include <splash.h>
  19. #if defined(CONFIG_SYS_I2C)
  20. #include <i2c.h>
  21. #endif
  22. #include <dm/device-internal.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. static struct stdio_dev devs;
  25. struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
  26. char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
  27. #if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
  28. #define CONFIG_SYS_DEVICE_NULLDEV 1
  29. #endif
  30. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  31. #define CONFIG_SYS_DEVICE_NULLDEV 1
  32. #endif
  33. #ifdef CONFIG_SYS_DEVICE_NULLDEV
  34. static void nulldev_putc(struct stdio_dev *dev, const char c)
  35. {
  36. /* nulldev is empty! */
  37. }
  38. static void nulldev_puts(struct stdio_dev *dev, const char *s)
  39. {
  40. /* nulldev is empty! */
  41. }
  42. static int nulldev_input(struct stdio_dev *dev)
  43. {
  44. /* nulldev is empty! */
  45. return 0;
  46. }
  47. #endif
  48. static void stdio_serial_putc(struct stdio_dev *dev, const char c)
  49. {
  50. serial_putc(c);
  51. }
  52. static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
  53. {
  54. serial_puts(s);
  55. }
  56. static int stdio_serial_getc(struct stdio_dev *dev)
  57. {
  58. return serial_getc();
  59. }
  60. static int stdio_serial_tstc(struct stdio_dev *dev)
  61. {
  62. return serial_tstc();
  63. }
  64. /**************************************************************************
  65. * SYSTEM DRIVERS
  66. **************************************************************************
  67. */
  68. static void drv_system_init (void)
  69. {
  70. struct stdio_dev dev;
  71. memset (&dev, 0, sizeof (dev));
  72. strcpy (dev.name, "serial");
  73. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  74. dev.putc = stdio_serial_putc;
  75. dev.puts = stdio_serial_puts;
  76. dev.getc = stdio_serial_getc;
  77. dev.tstc = stdio_serial_tstc;
  78. stdio_register (&dev);
  79. #ifdef CONFIG_SYS_DEVICE_NULLDEV
  80. memset (&dev, 0, sizeof (dev));
  81. strcpy (dev.name, "nulldev");
  82. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  83. dev.putc = nulldev_putc;
  84. dev.puts = nulldev_puts;
  85. dev.getc = nulldev_input;
  86. dev.tstc = nulldev_input;
  87. stdio_register (&dev);
  88. #endif
  89. }
  90. /**************************************************************************
  91. * DEVICES
  92. **************************************************************************
  93. */
  94. struct list_head* stdio_get_list(void)
  95. {
  96. return &(devs.list);
  97. }
  98. #ifdef CONFIG_DM_VIDEO
  99. /**
  100. * stdio_probe_device() - Find a device which provides the given stdio device
  101. *
  102. * This looks for a device of the given uclass which provides a particular
  103. * stdio device. It is currently really only useful for UCLASS_VIDEO.
  104. *
  105. * Ultimately we want to be able to probe a device by its stdio name. At
  106. * present devices register in their probe function (for video devices this
  107. * is done in vidconsole_post_probe()) and we don't know what name they will
  108. * use until they do so.
  109. * TODO(sjg@chromium.org): We should be able to determine the name before
  110. * probing, and probe the required device.
  111. *
  112. * @name: stdio device name (e.g. "vidconsole")
  113. * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
  114. * @sdevp: Returns stdout device, if found, else NULL
  115. * @return 0 if found, -ENOENT if no device found with that name, other -ve
  116. * on other error
  117. */
  118. static int stdio_probe_device(const char *name, enum uclass_id id,
  119. struct stdio_dev **sdevp)
  120. {
  121. struct stdio_dev *sdev;
  122. struct udevice *dev;
  123. int seq, ret;
  124. *sdevp = NULL;
  125. seq = trailing_strtoln(name, NULL);
  126. if (seq == -1)
  127. seq = 0;
  128. ret = uclass_get_device_by_seq(id, seq, &dev);
  129. if (ret == -ENODEV)
  130. ret = uclass_first_device_err(id, &dev);
  131. if (ret) {
  132. debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
  133. seq, name);
  134. return ret;
  135. }
  136. /* The device should be be the last one registered */
  137. sdev = list_empty(&devs.list) ? NULL :
  138. list_last_entry(&devs.list, struct stdio_dev, list);
  139. if (!sdev || strcmp(sdev->name, name)) {
  140. debug("Device '%s' did not register with stdio as '%s'\n",
  141. dev->name, name);
  142. return -ENOENT;
  143. }
  144. *sdevp = sdev;
  145. return 0;
  146. }
  147. #endif
  148. struct stdio_dev *stdio_get_by_name(const char *name)
  149. {
  150. struct list_head *pos;
  151. struct stdio_dev *sdev;
  152. if (!name)
  153. return NULL;
  154. list_for_each(pos, &(devs.list)) {
  155. sdev = list_entry(pos, struct stdio_dev, list);
  156. if (strcmp(sdev->name, name) == 0)
  157. return sdev;
  158. }
  159. #ifdef CONFIG_DM_VIDEO
  160. /*
  161. * We did not find a suitable stdio device. If there is a video
  162. * driver with a name starting with 'vidconsole', we can try probing
  163. * that in the hope that it will produce the required stdio device.
  164. *
  165. * This function is sometimes called with the entire value of
  166. * 'stdout', which may include a list of devices separate by commas.
  167. * Obviously this is not going to work, so we ignore that case. The
  168. * call path in that case is console_init_r() -> search_device() ->
  169. * stdio_get_by_name().
  170. */
  171. if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
  172. !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
  173. return sdev;
  174. #endif
  175. return NULL;
  176. }
  177. struct stdio_dev* stdio_clone(struct stdio_dev *dev)
  178. {
  179. struct stdio_dev *_dev;
  180. if(!dev)
  181. return NULL;
  182. _dev = calloc(1, sizeof(struct stdio_dev));
  183. if(!_dev)
  184. return NULL;
  185. memcpy(_dev, dev, sizeof(struct stdio_dev));
  186. return _dev;
  187. }
  188. int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
  189. {
  190. struct stdio_dev *_dev;
  191. _dev = stdio_clone(dev);
  192. if(!_dev)
  193. return -ENODEV;
  194. list_add_tail(&(_dev->list), &(devs.list));
  195. if (devp)
  196. *devp = _dev;
  197. return 0;
  198. }
  199. int stdio_register(struct stdio_dev *dev)
  200. {
  201. return stdio_register_dev(dev, NULL);
  202. }
  203. /* deregister the device "devname".
  204. * returns 0 if success, -1 if device is assigned and 1 if devname not found
  205. */
  206. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  207. int stdio_deregister_dev(struct stdio_dev *dev, int force)
  208. {
  209. int l;
  210. struct list_head *pos;
  211. char temp_names[3][16];
  212. /* get stdio devices (ListRemoveItem changes the dev list) */
  213. for (l=0 ; l< MAX_FILES; l++) {
  214. if (stdio_devices[l] == dev) {
  215. if (force) {
  216. strcpy(temp_names[l], "nulldev");
  217. continue;
  218. }
  219. /* Device is assigned -> report error */
  220. return -1;
  221. }
  222. memcpy (&temp_names[l][0],
  223. stdio_devices[l]->name,
  224. sizeof(temp_names[l]));
  225. }
  226. list_del(&(dev->list));
  227. free(dev);
  228. /* reassign Device list */
  229. list_for_each(pos, &(devs.list)) {
  230. dev = list_entry(pos, struct stdio_dev, list);
  231. for (l=0 ; l< MAX_FILES; l++) {
  232. if(strcmp(dev->name, temp_names[l]) == 0)
  233. stdio_devices[l] = dev;
  234. }
  235. }
  236. return 0;
  237. }
  238. int stdio_deregister(const char *devname, int force)
  239. {
  240. struct stdio_dev *dev;
  241. dev = stdio_get_by_name(devname);
  242. if (!dev) /* device not found */
  243. return -ENODEV;
  244. return stdio_deregister_dev(dev, force);
  245. }
  246. #endif /* CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) */
  247. int stdio_init_tables(void)
  248. {
  249. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  250. /* already relocated for current ARM implementation */
  251. ulong relocation_offset = gd->reloc_off;
  252. int i;
  253. /* relocate device name pointers */
  254. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  255. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  256. relocation_offset);
  257. }
  258. #endif /* CONFIG_NEEDS_MANUAL_RELOC */
  259. /* Initialize the list */
  260. INIT_LIST_HEAD(&(devs.list));
  261. return 0;
  262. }
  263. int stdio_add_devices(void)
  264. {
  265. #ifdef CONFIG_DM_KEYBOARD
  266. struct udevice *dev;
  267. struct uclass *uc;
  268. int ret;
  269. /*
  270. * For now we probe all the devices here. At some point this should be
  271. * done only when the devices are required - e.g. we have a list of
  272. * input devices to start up in the stdin environment variable. That
  273. * work probably makes more sense when stdio itself is converted to
  274. * driver model.
  275. *
  276. * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc.
  277. * to return the device even on error. Then we could use that here.
  278. */
  279. ret = uclass_get(UCLASS_KEYBOARD, &uc);
  280. if (ret)
  281. return ret;
  282. /* Don't report errors to the caller - assume that they are non-fatal */
  283. uclass_foreach_dev(dev, uc) {
  284. ret = device_probe(dev);
  285. if (ret)
  286. printf("Failed to probe keyboard '%s'\n", dev->name);
  287. }
  288. #endif
  289. #ifdef CONFIG_SYS_I2C
  290. i2c_init_all();
  291. #else
  292. #endif
  293. #ifdef CONFIG_DM_VIDEO
  294. /*
  295. * If the console setting is not in environment variables then
  296. * console_init_r() will not be calling iomux_doenv() (which calls
  297. * search_device()). So we will not dynamically add devices by
  298. * calling stdio_probe_device().
  299. *
  300. * So just probe all video devices now so that whichever one is
  301. * required will be available.
  302. */
  303. ulong start;
  304. start = get_timer(0);
  305. #ifndef CONFIG_SYS_CONSOLE_IS_IN_ENV
  306. struct udevice *vdev;
  307. # ifndef CONFIG_DM_KEYBOARD
  308. int ret;
  309. # endif
  310. for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
  311. vdev;
  312. ret = uclass_next_device(&vdev))
  313. ;
  314. if (ret)
  315. printf("%s: Video device failed (ret=%d)\n", __func__, ret);
  316. #endif /* !CONFIG_SYS_CONSOLE_IS_IN_ENV */
  317. #if defined(CONFIG_SPLASH_SCREEN) && defined(CONFIG_CMD_BMP)
  318. splash_display();
  319. #endif /* CONFIG_SPLASH_SCREEN && CONFIG_CMD_BMP */
  320. printf("splash screen startup cost %lu ms\n", get_timer(start));
  321. #else
  322. # if defined(CONFIG_LCD)
  323. drv_lcd_init ();
  324. # endif
  325. # if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
  326. drv_video_init ();
  327. # endif
  328. #endif /* CONFIG_DM_VIDEO */
  329. #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
  330. drv_keyboard_init ();
  331. #endif
  332. drv_system_init ();
  333. serial_stdio_init ();
  334. #ifdef CONFIG_USB_TTY
  335. drv_usbtty_init ();
  336. #endif
  337. #ifdef CONFIG_NETCONSOLE
  338. drv_nc_init ();
  339. #endif
  340. #ifdef CONFIG_JTAG_CONSOLE
  341. drv_jtag_console_init ();
  342. #endif
  343. #ifdef CONFIG_CBMEM_CONSOLE
  344. cbmemc_init();
  345. #endif
  346. return 0;
  347. }
  348. int stdio_init(void)
  349. {
  350. stdio_init_tables();
  351. stdio_add_devices();
  352. return 0;
  353. }