stdio.c 9.2 KB

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