stdio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /* deregister the device "devname".
  194. * returns 0 if success, -1 if device is assigned and 1 if devname not found
  195. */
  196. int stdio_deregister_dev(struct stdio_dev *dev, int force)
  197. {
  198. int l;
  199. struct list_head *pos;
  200. char temp_names[3][16];
  201. /* get stdio devices (ListRemoveItem changes the dev list) */
  202. for (l=0 ; l< MAX_FILES; l++) {
  203. if (stdio_devices[l] == dev) {
  204. if (force) {
  205. strcpy(temp_names[l], "nulldev");
  206. continue;
  207. }
  208. /* Device is assigned -> report error */
  209. return -1;
  210. }
  211. memcpy (&temp_names[l][0],
  212. stdio_devices[l]->name,
  213. sizeof(temp_names[l]));
  214. }
  215. list_del(&dev->list);
  216. free(dev);
  217. /* reassign Device list */
  218. list_for_each(pos, &devs.list) {
  219. dev = list_entry(pos, struct stdio_dev, list);
  220. for (l=0 ; l< MAX_FILES; l++) {
  221. if(strcmp(dev->name, temp_names[l]) == 0)
  222. stdio_devices[l] = dev;
  223. }
  224. }
  225. return 0;
  226. }
  227. int stdio_deregister(const char *devname, int force)
  228. {
  229. struct stdio_dev *dev;
  230. dev = stdio_get_by_name(devname);
  231. if (!dev) /* device not found */
  232. return -ENODEV;
  233. return stdio_deregister_dev(dev, force);
  234. }
  235. int stdio_init_tables(void)
  236. {
  237. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  238. /* already relocated for current ARM implementation */
  239. ulong relocation_offset = gd->reloc_off;
  240. int i;
  241. /* relocate device name pointers */
  242. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  243. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  244. relocation_offset);
  245. }
  246. #endif /* CONFIG_NEEDS_MANUAL_RELOC */
  247. /* Initialize the list */
  248. INIT_LIST_HEAD(&devs.list);
  249. return 0;
  250. }
  251. int stdio_add_devices(void)
  252. {
  253. struct udevice *dev;
  254. struct uclass *uc;
  255. int ret;
  256. if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
  257. /*
  258. * For now we probe all the devices here. At some point this
  259. * should be done only when the devices are required - e.g. we
  260. * have a list of input devices to start up in the stdin
  261. * environment variable. That work probably makes more sense
  262. * when stdio itself is converted to driver model.
  263. *
  264. * TODO(sjg@chromium.org): Convert changing
  265. * uclass_first_device() etc. to return the device even on
  266. * error. Then we could use that here.
  267. */
  268. ret = uclass_get(UCLASS_KEYBOARD, &uc);
  269. if (ret)
  270. return ret;
  271. /*
  272. * Don't report errors to the caller - assume that they are
  273. * non-fatal
  274. */
  275. uclass_foreach_dev(dev, uc) {
  276. ret = device_probe(dev);
  277. if (ret)
  278. printf("Failed to probe keyboard '%s'\n",
  279. dev->name);
  280. }
  281. }
  282. #ifdef CONFIG_SYS_I2C
  283. i2c_init_all();
  284. #endif
  285. if (IS_ENABLED(CONFIG_DM_VIDEO)) {
  286. /*
  287. * If the console setting is not in environment variables then
  288. * console_init_r() will not be calling iomux_doenv() (which
  289. * calls search_device()). So we will not dynamically add
  290. * devices by calling stdio_probe_device().
  291. *
  292. * So just probe all video devices now so that whichever one is
  293. * required will be available.
  294. */
  295. struct udevice *vdev;
  296. int ret;
  297. if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
  298. for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
  299. vdev;
  300. ret = uclass_next_device(&vdev))
  301. ;
  302. if (ret)
  303. printf("%s: Video device failed (ret=%d)\n",
  304. __func__, ret);
  305. }
  306. if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
  307. IS_ENABLED(CONFIG_CMD_BMP))
  308. splash_display();
  309. } else {
  310. if (IS_ENABLED(CONFIG_LCD))
  311. drv_lcd_init();
  312. if (IS_ENABLED(CONFIG_VIDEO) || IS_ENABLED(CONFIG_CFB_CONSOLE))
  313. drv_video_init();
  314. }
  315. #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
  316. drv_keyboard_init();
  317. #endif
  318. drv_system_init();
  319. serial_stdio_init();
  320. #ifdef CONFIG_USB_TTY
  321. drv_usbtty_init();
  322. #endif
  323. if (IS_ENABLED(CONFIG_NETCONSOLE))
  324. drv_nc_init();
  325. #ifdef CONFIG_JTAG_CONSOLE
  326. drv_jtag_console_init();
  327. #endif
  328. if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))
  329. cbmemc_init();
  330. return 0;
  331. }
  332. int stdio_init(void)
  333. {
  334. stdio_init_tables();
  335. stdio_add_devices();
  336. return 0;
  337. }