stdio.c 9.3 KB

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