2 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
4 * Changes for multibus/multiadapter I2C support.
7 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
9 * SPDX-License-Identifier: GPL-2.0+
18 #include <stdio_dev.h>
21 #if defined(CONFIG_SYS_I2C)
25 #include <dm/device-internal.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 static struct stdio_dev devs;
30 struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
31 char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
33 #if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
34 #define CONFIG_SYS_DEVICE_NULLDEV 1
37 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
38 #define CONFIG_SYS_DEVICE_NULLDEV 1
41 #ifdef CONFIG_SYS_DEVICE_NULLDEV
42 static void nulldev_putc(struct stdio_dev *dev, const char c)
44 /* nulldev is empty! */
47 static void nulldev_puts(struct stdio_dev *dev, const char *s)
49 /* nulldev is empty! */
52 static int nulldev_input(struct stdio_dev *dev)
54 /* nulldev is empty! */
59 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
64 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
69 static int stdio_serial_getc(struct stdio_dev *dev)
74 static int stdio_serial_tstc(struct stdio_dev *dev)
79 /**************************************************************************
81 **************************************************************************
84 static void drv_system_init (void)
88 memset (&dev, 0, sizeof (dev));
90 strcpy (dev.name, "serial");
91 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
92 dev.putc = stdio_serial_putc;
93 dev.puts = stdio_serial_puts;
94 dev.getc = stdio_serial_getc;
95 dev.tstc = stdio_serial_tstc;
96 stdio_register (&dev);
98 #ifdef CONFIG_SYS_DEVICE_NULLDEV
99 memset (&dev, 0, sizeof (dev));
101 strcpy (dev.name, "nulldev");
102 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
103 dev.putc = nulldev_putc;
104 dev.puts = nulldev_puts;
105 dev.getc = nulldev_input;
106 dev.tstc = nulldev_input;
108 stdio_register (&dev);
112 /**************************************************************************
114 **************************************************************************
116 struct list_head* stdio_get_list(void)
121 #ifdef CONFIG_DM_VIDEO
123 * stdio_probe_device() - Find a device which provides the given stdio device
125 * This looks for a device of the given uclass which provides a particular
126 * stdio device. It is currently really only useful for UCLASS_VIDEO.
128 * Ultimately we want to be able to probe a device by its stdio name. At
129 * present devices register in their probe function (for video devices this
130 * is done in vidconsole_post_probe()) and we don't know what name they will
131 * use until they do so.
132 * TODO(sjg@chromium.org): We should be able to determine the name before
133 * probing, and probe the required device.
135 * @name: stdio device name (e.g. "vidconsole")
136 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
137 * @sdevp: Returns stdout device, if found, else NULL
138 * @return 0 if found, -ENOENT if no device found with that name, other -ve
141 static int stdio_probe_device(const char *name, enum uclass_id id,
142 struct stdio_dev **sdevp)
144 struct stdio_dev *sdev;
149 seq = trailing_strtoln(name, NULL);
152 ret = uclass_get_device_by_seq(id, seq, &dev);
154 ret = uclass_first_device_err(id, &dev);
156 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
160 /* The device should be be the last one registered */
161 sdev = list_empty(&devs.list) ? NULL :
162 list_last_entry(&devs.list, struct stdio_dev, list);
163 if (!sdev || strcmp(sdev->name, name)) {
164 debug("Device '%s' did not register with stdio as '%s'\n",
174 struct stdio_dev *stdio_get_by_name(const char *name)
176 struct list_head *pos;
177 struct stdio_dev *sdev;
182 list_for_each(pos, &(devs.list)) {
183 sdev = list_entry(pos, struct stdio_dev, list);
184 if (strcmp(sdev->name, name) == 0)
187 #ifdef CONFIG_DM_VIDEO
189 * We did not find a suitable stdio device. If there is a video
190 * driver with a name starting with 'vidconsole', we can try probing
191 * that in the hope that it will produce the required stdio device.
193 * This function is sometimes called with the entire value of
194 * 'stdout', which may include a list of devices separate by commas.
195 * Obviously this is not going to work, so we ignore that case. The
196 * call path in that case is console_init_r() -> search_device() ->
197 * stdio_get_by_name().
199 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
200 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
207 struct stdio_dev* stdio_clone(struct stdio_dev *dev)
209 struct stdio_dev *_dev;
214 _dev = calloc(1, sizeof(struct stdio_dev));
219 memcpy(_dev, dev, sizeof(struct stdio_dev));
224 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
226 struct stdio_dev *_dev;
228 _dev = stdio_clone(dev);
231 list_add_tail(&(_dev->list), &(devs.list));
238 int stdio_register(struct stdio_dev *dev)
240 return stdio_register_dev(dev, NULL);
243 /* deregister the device "devname".
244 * returns 0 if success, -1 if device is assigned and 1 if devname not found
246 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
247 int stdio_deregister_dev(struct stdio_dev *dev, int force)
250 struct list_head *pos;
251 char temp_names[3][16];
253 /* get stdio devices (ListRemoveItem changes the dev list) */
254 for (l=0 ; l< MAX_FILES; l++) {
255 if (stdio_devices[l] == dev) {
257 strcpy(temp_names[l], "nulldev");
260 /* Device is assigned -> report error */
263 memcpy (&temp_names[l][0],
264 stdio_devices[l]->name,
265 sizeof(temp_names[l]));
268 list_del(&(dev->list));
271 /* reassign Device list */
272 list_for_each(pos, &(devs.list)) {
273 dev = list_entry(pos, struct stdio_dev, list);
274 for (l=0 ; l< MAX_FILES; l++) {
275 if(strcmp(dev->name, temp_names[l]) == 0)
276 stdio_devices[l] = dev;
282 int stdio_deregister(const char *devname, int force)
284 struct stdio_dev *dev;
286 dev = stdio_get_by_name(devname);
288 if (!dev) /* device not found */
291 return stdio_deregister_dev(dev, force);
293 #endif /* CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) */
295 int stdio_init_tables(void)
297 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
298 /* already relocated for current ARM implementation */
299 ulong relocation_offset = gd->reloc_off;
302 /* relocate device name pointers */
303 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
304 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
307 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
309 /* Initialize the list */
310 INIT_LIST_HEAD(&(devs.list));
315 int stdio_add_devices(void)
317 #ifdef CONFIG_DM_KEYBOARD
323 * For now we probe all the devices here. At some point this should be
324 * done only when the devices are required - e.g. we have a list of
325 * input devices to start up in the stdin environment variable. That
326 * work probably makes more sense when stdio itself is converted to
329 * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc.
330 * to return the device even on error. Then we could use that here.
332 ret = uclass_get(UCLASS_KEYBOARD, &uc);
336 /* Don't report errors to the caller - assume that they are non-fatal */
337 uclass_foreach_dev(dev, uc) {
338 ret = device_probe(dev);
340 printf("Failed to probe keyboard '%s'\n", dev->name);
343 #ifdef CONFIG_SYS_I2C
347 #ifdef CONFIG_DM_VIDEO
349 * If the console setting is not in environment variables then
350 * console_init_r() will not be calling iomux_doenv() (which calls
351 * search_device()). So we will not dynamically add devices by
352 * calling stdio_probe_device().
354 * So just probe all video devices now so that whichever one is
355 * required will be available.
357 #ifndef CONFIG_SYS_CONSOLE_IS_IN_ENV
358 struct udevice *vdev;
359 # ifndef CONFIG_DM_KEYBOARD
363 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
365 ret = uclass_next_device(&vdev))
368 printf("%s: Video device failed (ret=%d)\n", __func__, ret);
369 #endif /* !CONFIG_SYS_CONSOLE_IS_IN_ENV */
371 # if defined(CONFIG_LCD)
374 # if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
377 #endif /* CONFIG_DM_VIDEO */
378 #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
379 drv_keyboard_init ();
382 serial_stdio_init ();
383 #ifdef CONFIG_USB_TTY
386 #ifdef CONFIG_NETCONSOLE
389 #ifdef CONFIG_JTAG_CONSOLE
390 drv_jtag_console_init ();
392 #ifdef CONFIG_CBMEM_CONSOLE