1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
5 * Changes for multibus/multiadapter I2C support.
8 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
18 #include <stdio_dev.h>
22 #if defined(CONFIG_SYS_I2C)
26 #include <dm/device-internal.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 static struct stdio_dev devs;
31 struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
32 char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
34 static void nulldev_putc(struct stdio_dev *dev, const char c)
36 /* nulldev is empty! */
39 static void nulldev_puts(struct stdio_dev *dev, const char *s)
41 /* nulldev is empty! */
44 static int nulldev_input(struct stdio_dev *dev)
46 /* nulldev is empty! */
50 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
55 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
60 static int stdio_serial_getc(struct stdio_dev *dev)
65 static int stdio_serial_tstc(struct stdio_dev *dev)
70 /**************************************************************************
72 **************************************************************************
75 static void drv_system_init (void)
79 memset (&dev, 0, sizeof (dev));
81 strcpy (dev.name, "serial");
82 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
83 dev.putc = stdio_serial_putc;
84 dev.puts = stdio_serial_puts;
85 dev.getc = stdio_serial_getc;
86 dev.tstc = stdio_serial_tstc;
87 stdio_register (&dev);
89 if (CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)) {
90 memset(&dev, '\0', sizeof(dev));
92 strcpy(dev.name, "nulldev");
93 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
94 dev.putc = nulldev_putc;
95 dev.puts = nulldev_puts;
96 dev.getc = nulldev_input;
97 dev.tstc = nulldev_input;
103 /**************************************************************************
105 **************************************************************************
107 struct list_head* stdio_get_list(void)
112 #ifdef CONFIG_DM_VIDEO
114 * stdio_probe_device() - Find a device which provides the given stdio device
116 * This looks for a device of the given uclass which provides a particular
117 * stdio device. It is currently really only useful for UCLASS_VIDEO.
119 * Ultimately we want to be able to probe a device by its stdio name. At
120 * present devices register in their probe function (for video devices this
121 * is done in vidconsole_post_probe()) and we don't know what name they will
122 * use until they do so.
123 * TODO(sjg@chromium.org): We should be able to determine the name before
124 * probing, and probe the required device.
126 * @name: stdio device name (e.g. "vidconsole")
127 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
128 * @sdevp: Returns stdout device, if found, else NULL
129 * @return 0 if found, -ENOENT if no device found with that name, other -ve
132 static int stdio_probe_device(const char *name, enum uclass_id id,
133 struct stdio_dev **sdevp)
135 struct stdio_dev *sdev;
140 seq = trailing_strtoln(name, NULL);
143 ret = uclass_get_device_by_seq(id, seq, &dev);
145 ret = uclass_first_device_err(id, &dev);
147 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
151 /* The device should be be the last one registered */
152 sdev = list_empty(&devs.list) ? NULL :
153 list_last_entry(&devs.list, struct stdio_dev, list);
154 if (!sdev || strcmp(sdev->name, name)) {
155 debug("Device '%s' did not register with stdio as '%s'\n",
165 struct stdio_dev *stdio_get_by_name(const char *name)
167 struct list_head *pos;
168 struct stdio_dev *sdev;
173 list_for_each(pos, &(devs.list)) {
174 sdev = list_entry(pos, struct stdio_dev, list);
175 if (strcmp(sdev->name, name) == 0)
178 #ifdef CONFIG_DM_VIDEO
180 * We did not find a suitable stdio device. If there is a video
181 * driver with a name starting with 'vidconsole', we can try probing
182 * that in the hope that it will produce the required stdio device.
184 * This function is sometimes called with the entire value of
185 * 'stdout', which may include a list of devices separate by commas.
186 * Obviously this is not going to work, so we ignore that case. The
187 * call path in that case is console_init_r() -> search_device() ->
188 * stdio_get_by_name().
190 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
191 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
198 struct stdio_dev* stdio_clone(struct stdio_dev *dev)
200 struct stdio_dev *_dev;
205 _dev = calloc(1, sizeof(struct stdio_dev));
210 memcpy(_dev, dev, sizeof(struct stdio_dev));
215 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
217 struct stdio_dev *_dev;
219 _dev = stdio_clone(dev);
222 list_add_tail(&(_dev->list), &(devs.list));
229 int stdio_register(struct stdio_dev *dev)
231 return stdio_register_dev(dev, NULL);
234 /* deregister the device "devname".
235 * returns 0 if success, -1 if device is assigned and 1 if devname not found
237 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
238 int stdio_deregister_dev(struct stdio_dev *dev, int force)
241 struct list_head *pos;
242 char temp_names[3][16];
244 /* get stdio devices (ListRemoveItem changes the dev list) */
245 for (l=0 ; l< MAX_FILES; l++) {
246 if (stdio_devices[l] == dev) {
248 strcpy(temp_names[l], "nulldev");
251 /* Device is assigned -> report error */
254 memcpy (&temp_names[l][0],
255 stdio_devices[l]->name,
256 sizeof(temp_names[l]));
259 list_del(&(dev->list));
262 /* reassign Device list */
263 list_for_each(pos, &(devs.list)) {
264 dev = list_entry(pos, struct stdio_dev, list);
265 for (l=0 ; l< MAX_FILES; l++) {
266 if(strcmp(dev->name, temp_names[l]) == 0)
267 stdio_devices[l] = dev;
273 int stdio_deregister(const char *devname, int force)
275 struct stdio_dev *dev;
277 dev = stdio_get_by_name(devname);
279 if (!dev) /* device not found */
282 return stdio_deregister_dev(dev, force);
284 #endif /* CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) */
286 int stdio_init_tables(void)
288 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
289 /* already relocated for current ARM implementation */
290 ulong relocation_offset = gd->reloc_off;
293 /* relocate device name pointers */
294 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
295 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
298 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
300 /* Initialize the list */
301 INIT_LIST_HEAD(&(devs.list));
306 int stdio_add_devices(void)
308 #ifdef CONFIG_DM_KEYBOARD
314 * For now we probe all the devices here. At some point this should be
315 * done only when the devices are required - e.g. we have a list of
316 * input devices to start up in the stdin environment variable. That
317 * work probably makes more sense when stdio itself is converted to
320 * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc.
321 * to return the device even on error. Then we could use that here.
323 ret = uclass_get(UCLASS_KEYBOARD, &uc);
327 /* Don't report errors to the caller - assume that they are non-fatal */
328 uclass_foreach_dev(dev, uc) {
329 ret = device_probe(dev);
331 printf("Failed to probe keyboard '%s'\n", dev->name);
334 #ifdef CONFIG_SYS_I2C
338 #ifdef CONFIG_DM_VIDEO
340 * If the console setting is not in environment variables then
341 * console_init_r() will not be calling iomux_doenv() (which calls
342 * search_device()). So we will not dynamically add devices by
343 * calling stdio_probe_device().
345 * So just probe all video devices now so that whichever one is
346 * required will be available.
348 #ifndef CONFIG_SYS_CONSOLE_IS_IN_ENV
349 struct udevice *vdev;
350 # ifndef CONFIG_DM_KEYBOARD
354 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
356 ret = uclass_next_device(&vdev))
359 printf("%s: Video device failed (ret=%d)\n", __func__, ret);
360 #endif /* !CONFIG_SYS_CONSOLE_IS_IN_ENV */
361 #if defined(CONFIG_SPLASH_SCREEN) && defined(CONFIG_CMD_BMP)
363 #endif /* CONFIG_SPLASH_SCREEN && CONFIG_CMD_BMP */
365 # if defined(CONFIG_LCD)
368 # if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
371 #endif /* CONFIG_DM_VIDEO */
372 #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
373 drv_keyboard_init ();
376 serial_stdio_init ();
377 #ifdef CONFIG_USB_TTY
380 #ifdef CONFIG_NETCONSOLE
383 #ifdef CONFIG_JTAG_CONSOLE
384 drv_jtag_console_init ();
386 #ifdef CONFIG_CBMEM_CONSOLE