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 #include <asm/global_data.h>
23 #include <dm/device-internal.h>
25 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" };
31 int stdio_file_to_flags(const int file)
35 return DEV_FLAGS_INPUT;
38 return DEV_FLAGS_OUTPUT;
44 #if CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)
45 static void nulldev_putc(struct stdio_dev *dev, const char c)
47 /* nulldev is empty! */
50 static void nulldev_puts(struct stdio_dev *dev, const char *s)
52 /* nulldev is empty! */
55 static int nulldev_input(struct stdio_dev *dev)
57 /* nulldev is empty! */
61 static void nulldev_register(void)
65 memset(&dev, '\0', sizeof(dev));
67 strcpy(dev.name, "nulldev");
68 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
69 dev.putc = nulldev_putc;
70 dev.puts = nulldev_puts;
71 dev.getc = nulldev_input;
72 dev.tstc = nulldev_input;
77 static inline void nulldev_register(void) {}
78 #endif /* SYS_DEVICE_NULLDEV */
80 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
85 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
90 static int stdio_serial_getc(struct stdio_dev *dev)
95 static int stdio_serial_tstc(struct stdio_dev *dev)
100 /**************************************************************************
102 **************************************************************************
105 static void drv_system_init (void)
107 struct stdio_dev dev;
109 memset (&dev, 0, sizeof (dev));
111 strcpy (dev.name, "serial");
112 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
113 dev.putc = stdio_serial_putc;
114 dev.puts = stdio_serial_puts;
115 dev.getc = stdio_serial_getc;
116 dev.tstc = stdio_serial_tstc;
117 stdio_register (&dev);
122 /**************************************************************************
124 **************************************************************************
126 struct list_head* stdio_get_list(void)
132 * stdio_probe_device() - Find a device which provides the given stdio device
134 * This looks for a device of the given uclass which provides a particular
135 * stdio device. It is currently really only useful for UCLASS_VIDEO.
137 * Ultimately we want to be able to probe a device by its stdio name. At
138 * present devices register in their probe function (for video devices this
139 * is done in vidconsole_post_probe()) and we don't know what name they will
140 * use until they do so.
141 * TODO(sjg@chromium.org): We should be able to determine the name before
142 * probing, and probe the required device.
144 * @name: stdio device name (e.g. "vidconsole")
145 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
146 * @sdevp: Returns stdout device, if found, else NULL
147 * Return: 0 if found, -ENOENT if no device found with that name, other -ve
150 static int stdio_probe_device(const char *name, enum uclass_id id,
151 struct stdio_dev **sdevp)
153 struct stdio_dev *sdev;
158 seq = trailing_strtoln(name, NULL);
161 ret = uclass_get_device_by_seq(id, seq, &dev);
163 ret = uclass_first_device_err(id, &dev);
165 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
169 /* The device should be be the last one registered */
170 sdev = list_empty(&devs.list) ? NULL :
171 list_last_entry(&devs.list, struct stdio_dev, list);
172 if (!sdev || strcmp(sdev->name, name)) {
173 debug("Device '%s' did not register with stdio as '%s'\n",
182 struct stdio_dev *stdio_get_by_name(const char *name)
184 struct list_head *pos;
185 struct stdio_dev *sdev;
190 list_for_each(pos, &devs.list) {
191 sdev = list_entry(pos, struct stdio_dev, list);
192 if (strcmp(sdev->name, name) == 0)
195 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
197 * We did not find a suitable stdio device. If there is a video
198 * driver with a name starting with 'vidconsole', we can try
199 * probing that in the hope that it will produce the required
202 * This function is sometimes called with the entire value of
203 * 'stdout', which may include a list of devices separate by
204 * commas. Obviously this is not going to work, so we ignore
205 * that case. The call path in that case is
206 * console_init_r() -> console_search_dev() -> stdio_get_by_name()
208 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
209 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
216 struct stdio_dev *stdio_clone(struct stdio_dev *dev)
218 struct stdio_dev *_dev;
223 _dev = calloc(1, sizeof(struct stdio_dev));
227 memcpy(_dev, dev, sizeof(struct stdio_dev));
232 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
234 struct stdio_dev *_dev;
236 _dev = stdio_clone(dev);
239 list_add_tail(&_dev->list, &devs.list);
246 int stdio_register(struct stdio_dev *dev)
248 return stdio_register_dev(dev, NULL);
251 int stdio_deregister_dev(struct stdio_dev *dev, int force)
253 struct list_head *pos;
254 char temp_names[3][16];
257 /* get stdio devices (ListRemoveItem changes the dev list) */
258 for (i = 0 ; i < MAX_FILES; i++) {
259 if (stdio_devices[i] == dev) {
261 strcpy(temp_names[i], "nulldev");
264 /* Device is assigned -> report error */
267 memcpy(&temp_names[i][0], stdio_devices[i]->name,
268 sizeof(temp_names[i]));
271 list_del(&dev->list);
274 /* reassign device list */
275 list_for_each(pos, &devs.list) {
276 dev = list_entry(pos, struct stdio_dev, list);
277 for (i = 0 ; i < MAX_FILES; i++) {
278 if (strcmp(dev->name, temp_names[i]) == 0)
279 stdio_devices[i] = dev;
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)
312 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
314 * For now we probe all the devices here. At some point this
315 * should be done only when the devices are required - e.g. we
316 * have a list of input devices to start up in the stdin
317 * environment variable. That work probably makes more sense
318 * when stdio itself is converted to driver model.
320 * TODO(sjg@chromium.org): Convert changing
321 * uclass_first_device() etc. to return the device even on
322 * error. Then we could use that here.
324 ret = uclass_get(UCLASS_KEYBOARD, &uc);
329 * Don't report errors to the caller - assume that they are
332 uclass_foreach_dev(dev, uc) {
333 ret = device_probe(dev);
335 printf("Failed to probe keyboard '%s'\n",
339 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
342 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
344 * If the console setting is not in environment variables then
345 * console_init_r() will not be calling iomux_doenv() (which
346 * calls console_search_dev()). So we will not dynamically add
347 * devices by calling stdio_probe_device().
349 * So just probe all video devices now so that whichever one is
350 * required will be available.
352 struct udevice *vdev;
355 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
356 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
358 ret = uclass_next_device(&vdev))
361 printf("%s: Video device failed (ret=%d)\n",
364 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
365 IS_ENABLED(CONFIG_CMD_BMP))
368 if (IS_ENABLED(CONFIG_LCD))
370 if (IS_ENABLED(CONFIG_VIDEO) ||
371 IS_ENABLED(CONFIG_VIDEO_VCXK))
375 #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
380 #ifdef CONFIG_USB_TTY
383 #ifdef CONFIG_USB_FUNCTION_ACM
386 if (IS_ENABLED(CONFIG_NETCONSOLE))
388 #ifdef CONFIG_JTAG_CONSOLE
389 drv_jtag_console_init();
391 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))