stdio.c 9.6 KB

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