stdio.c 9.6 KB

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