stdio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #ifdef CONFIG_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. ret = uclass_first_device_err(id, &dev);
  131. else
  132. ret = uclass_get_device_by_seq(id, seq, &dev);
  133. if (ret) {
  134. debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
  135. seq, name);
  136. return ret;
  137. }
  138. /* The device should be be the last one registered */
  139. sdev = list_empty(&devs.list) ? NULL :
  140. list_last_entry(&devs.list, struct stdio_dev, list);
  141. if (!sdev || strcmp(sdev->name, name)) {
  142. debug("Device '%s' did not register with stdio as '%s'\n",
  143. dev->name, name);
  144. return -ENOENT;
  145. }
  146. *sdevp = sdev;
  147. return 0;
  148. }
  149. #endif
  150. struct stdio_dev* stdio_get_by_name(const char *name)
  151. {
  152. struct list_head *pos;
  153. struct stdio_dev *sdev;
  154. if(!name)
  155. return NULL;
  156. list_for_each(pos, &(devs.list)) {
  157. sdev = list_entry(pos, struct stdio_dev, list);
  158. if (strcmp(sdev->name, name) == 0)
  159. return sdev;
  160. }
  161. #ifdef CONFIG_DM_VIDEO
  162. /*
  163. * We did not find a suitable stdio device. If there is a video
  164. * driver with a name starting with 'vidconsole', we can try probing
  165. * that in the hope that it will produce the required stdio device.
  166. *
  167. * This function is sometimes called with the entire value of
  168. * 'stdout', which may include a list of devices separate by commas.
  169. * Obviously this is not going to work, so we ignore that case. The
  170. * call path in that case is console_init_r() -> search_device() ->
  171. * stdio_get_by_name().
  172. */
  173. if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
  174. !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
  175. return sdev;
  176. #endif
  177. return NULL;
  178. }
  179. struct stdio_dev* stdio_clone(struct stdio_dev *dev)
  180. {
  181. struct stdio_dev *_dev;
  182. if(!dev)
  183. return NULL;
  184. _dev = calloc(1, sizeof(struct stdio_dev));
  185. if(!_dev)
  186. return NULL;
  187. memcpy(_dev, dev, sizeof(struct stdio_dev));
  188. return _dev;
  189. }
  190. int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
  191. {
  192. struct stdio_dev *_dev;
  193. _dev = stdio_clone(dev);
  194. if(!_dev)
  195. return -ENODEV;
  196. list_add_tail(&(_dev->list), &(devs.list));
  197. if (devp)
  198. *devp = _dev;
  199. return 0;
  200. }
  201. int stdio_register(struct stdio_dev *dev)
  202. {
  203. return stdio_register_dev(dev, NULL);
  204. }
  205. /* deregister the device "devname".
  206. * returns 0 if success, -1 if device is assigned and 1 if devname not found
  207. */
  208. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  209. int stdio_deregister_dev(struct stdio_dev *dev, int force)
  210. {
  211. int l;
  212. struct list_head *pos;
  213. char temp_names[3][16];
  214. /* get stdio devices (ListRemoveItem changes the dev list) */
  215. for (l=0 ; l< MAX_FILES; l++) {
  216. if (stdio_devices[l] == dev) {
  217. if (force) {
  218. strcpy(temp_names[l], "nulldev");
  219. continue;
  220. }
  221. /* Device is assigned -> report error */
  222. return -1;
  223. }
  224. memcpy (&temp_names[l][0],
  225. stdio_devices[l]->name,
  226. sizeof(temp_names[l]));
  227. }
  228. list_del(&(dev->list));
  229. free(dev);
  230. /* reassign Device list */
  231. list_for_each(pos, &(devs.list)) {
  232. dev = list_entry(pos, struct stdio_dev, list);
  233. for (l=0 ; l< MAX_FILES; l++) {
  234. if(strcmp(dev->name, temp_names[l]) == 0)
  235. stdio_devices[l] = dev;
  236. }
  237. }
  238. return 0;
  239. }
  240. int stdio_deregister(const char *devname, int force)
  241. {
  242. struct stdio_dev *dev;
  243. dev = stdio_get_by_name(devname);
  244. if (!dev) /* device not found */
  245. return -ENODEV;
  246. return stdio_deregister_dev(dev, force);
  247. }
  248. #endif /* CONFIG_SYS_STDIO_DEREGISTER */
  249. int stdio_init_tables(void)
  250. {
  251. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  252. /* already relocated for current ARM implementation */
  253. ulong relocation_offset = gd->reloc_off;
  254. int i;
  255. /* relocate device name pointers */
  256. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  257. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  258. relocation_offset);
  259. }
  260. #endif /* CONFIG_NEEDS_MANUAL_RELOC */
  261. /* Initialize the list */
  262. INIT_LIST_HEAD(&(devs.list));
  263. return 0;
  264. }
  265. int stdio_add_devices(void)
  266. {
  267. #ifdef CONFIG_DM_KEYBOARD
  268. struct udevice *dev;
  269. struct uclass *uc;
  270. int ret;
  271. /*
  272. * For now we probe all the devices here. At some point this should be
  273. * done only when the devices are required - e.g. we have a list of
  274. * input devices to start up in the stdin environment variable. That
  275. * work probably makes more sense when stdio itself is converted to
  276. * driver model.
  277. *
  278. * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc.
  279. * to return the device even on error. Then we could use that here.
  280. */
  281. ret = uclass_get(UCLASS_KEYBOARD, &uc);
  282. if (ret)
  283. return ret;
  284. /* Don't report errors to the caller - assume that they are non-fatal */
  285. uclass_foreach_dev(dev, uc) {
  286. ret = device_probe(dev);
  287. if (ret)
  288. printf("Failed to probe keyboard '%s'\n", dev->name);
  289. }
  290. #endif
  291. #ifdef CONFIG_SYS_I2C
  292. i2c_init_all();
  293. #else
  294. #if defined(CONFIG_HARD_I2C)
  295. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  296. #endif
  297. #endif
  298. #ifdef CONFIG_DM_VIDEO
  299. /*
  300. * If the console setting is not in environment variables then
  301. * console_init_r() will not be calling iomux_doenv() (which calls
  302. * search_device()). So we will not dynamically add devices by
  303. * calling stdio_probe_device().
  304. *
  305. * So just probe all video devices now so that whichever one is
  306. * required will be available.
  307. */
  308. #ifndef CONFIG_SYS_CONSOLE_IS_IN_ENV
  309. struct udevice *vdev;
  310. # ifndef CONFIG_DM_KEYBOARD
  311. int ret;
  312. # endif
  313. for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
  314. vdev;
  315. ret = uclass_next_device(&vdev))
  316. ;
  317. if (ret)
  318. printf("%s: Video device failed (ret=%d)\n", __func__, ret);
  319. #endif /* !CONFIG_SYS_CONSOLE_IS_IN_ENV */
  320. #else
  321. # if defined(CONFIG_LCD)
  322. drv_lcd_init ();
  323. # endif
  324. # if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
  325. drv_video_init ();
  326. # endif
  327. #endif /* CONFIG_DM_VIDEO */
  328. #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
  329. drv_keyboard_init ();
  330. #endif
  331. #ifdef CONFIG_LOGBUFFER
  332. drv_logbuff_init ();
  333. #endif
  334. drv_system_init ();
  335. serial_stdio_init ();
  336. #ifdef CONFIG_USB_TTY
  337. drv_usbtty_init ();
  338. #endif
  339. #ifdef CONFIG_NETCONSOLE
  340. drv_nc_init ();
  341. #endif
  342. #ifdef CONFIG_JTAG_CONSOLE
  343. drv_jtag_console_init ();
  344. #endif
  345. #ifdef CONFIG_CBMEM_CONSOLE
  346. cbmemc_init();
  347. #endif
  348. return 0;
  349. }
  350. int stdio_init(void)
  351. {
  352. stdio_init_tables();
  353. stdio_add_devices();
  354. return 0;
  355. }