stdio.c 9.1 KB

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