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 #if CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)
32 static void nulldev_putc(struct stdio_dev *dev, const char c)
34 /* nulldev is empty! */
37 static void nulldev_puts(struct stdio_dev *dev, const char *s)
39 /* nulldev is empty! */
42 static int nulldev_input(struct stdio_dev *dev)
44 /* nulldev is empty! */
48 static void nulldev_register(void)
52 memset(&dev, '\0', sizeof(dev));
54 strcpy(dev.name, "nulldev");
55 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
56 dev.putc = nulldev_putc;
57 dev.puts = nulldev_puts;
58 dev.getc = nulldev_input;
59 dev.tstc = nulldev_input;
64 static inline void nulldev_register(void) {}
65 #endif /* SYS_DEVICE_NULLDEV */
67 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
72 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
77 static int stdio_serial_getc(struct stdio_dev *dev)
82 static int stdio_serial_tstc(struct stdio_dev *dev)
87 /**************************************************************************
89 **************************************************************************
92 static void drv_system_init (void)
96 memset (&dev, 0, sizeof (dev));
98 strcpy (dev.name, "serial");
99 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
100 dev.putc = stdio_serial_putc;
101 dev.puts = stdio_serial_puts;
102 dev.getc = stdio_serial_getc;
103 dev.tstc = stdio_serial_tstc;
104 stdio_register (&dev);
109 /**************************************************************************
111 **************************************************************************
113 struct list_head* stdio_get_list(void)
119 * stdio_probe_device() - Find a device which provides the given stdio device
121 * This looks for a device of the given uclass which provides a particular
122 * stdio device. It is currently really only useful for UCLASS_VIDEO.
124 * Ultimately we want to be able to probe a device by its stdio name. At
125 * present devices register in their probe function (for video devices this
126 * is done in vidconsole_post_probe()) and we don't know what name they will
127 * use until they do so.
128 * TODO(sjg@chromium.org): We should be able to determine the name before
129 * probing, and probe the required device.
131 * @name: stdio device name (e.g. "vidconsole")
132 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
133 * @sdevp: Returns stdout device, if found, else NULL
134 * @return 0 if found, -ENOENT if no device found with that name, other -ve
137 static int stdio_probe_device(const char *name, enum uclass_id id,
138 struct stdio_dev **sdevp)
140 struct stdio_dev *sdev;
145 seq = trailing_strtoln(name, NULL);
148 ret = uclass_get_device_by_seq(id, seq, &dev);
150 ret = uclass_first_device_err(id, &dev);
152 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
156 /* The device should be be the last one registered */
157 sdev = list_empty(&devs.list) ? NULL :
158 list_last_entry(&devs.list, struct stdio_dev, list);
159 if (!sdev || strcmp(sdev->name, name)) {
160 debug("Device '%s' did not register with stdio as '%s'\n",
169 struct stdio_dev *stdio_get_by_name(const char *name)
171 struct list_head *pos;
172 struct stdio_dev *sdev;
177 list_for_each(pos, &devs.list) {
178 sdev = list_entry(pos, struct stdio_dev, list);
179 if (strcmp(sdev->name, name) == 0)
182 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
184 * We did not find a suitable stdio device. If there is a video
185 * driver with a name starting with 'vidconsole', we can try
186 * probing that in the hope that it will produce the required
189 * This function is sometimes called with the entire value of
190 * 'stdout', which may include a list of devices separate by
191 * commas. Obviously this is not going to work, so we ignore
192 * that case. The call path in that case is
193 * console_init_r() -> console_search_dev() -> stdio_get_by_name()
195 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
196 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
203 struct stdio_dev *stdio_clone(struct stdio_dev *dev)
205 struct stdio_dev *_dev;
210 _dev = calloc(1, sizeof(struct stdio_dev));
214 memcpy(_dev, dev, sizeof(struct stdio_dev));
219 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
221 struct stdio_dev *_dev;
223 _dev = stdio_clone(dev);
226 list_add_tail(&_dev->list, &devs.list);
233 int stdio_register(struct stdio_dev *dev)
235 return stdio_register_dev(dev, NULL);
238 int stdio_deregister_dev(struct stdio_dev *dev, int force)
240 struct list_head *pos;
241 char temp_names[3][16];
244 /* get stdio devices (ListRemoveItem changes the dev list) */
245 for (i = 0 ; i < MAX_FILES; i++) {
246 if (stdio_devices[i] == dev) {
248 strcpy(temp_names[i], "nulldev");
251 /* Device is assigned -> report error */
254 memcpy(&temp_names[i][0], stdio_devices[i]->name,
255 sizeof(temp_names[i]));
258 list_del(&dev->list);
261 /* reassign device list */
262 list_for_each(pos, &devs.list) {
263 dev = list_entry(pos, struct stdio_dev, list);
264 for (i = 0 ; i < MAX_FILES; i++) {
265 if (strcmp(dev->name, temp_names[i]) == 0)
266 stdio_devices[i] = dev;
273 int stdio_init_tables(void)
275 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
276 /* already relocated for current ARM implementation */
277 ulong relocation_offset = gd->reloc_off;
280 /* relocate device name pointers */
281 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
282 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
285 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
287 /* Initialize the list */
288 INIT_LIST_HEAD(&devs.list);
293 int stdio_add_devices(void)
299 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
301 * For now we probe all the devices here. At some point this
302 * should be done only when the devices are required - e.g. we
303 * have a list of input devices to start up in the stdin
304 * environment variable. That work probably makes more sense
305 * when stdio itself is converted to driver model.
307 * TODO(sjg@chromium.org): Convert changing
308 * uclass_first_device() etc. to return the device even on
309 * error. Then we could use that here.
311 ret = uclass_get(UCLASS_KEYBOARD, &uc);
316 * Don't report errors to the caller - assume that they are
319 uclass_foreach_dev(dev, uc) {
320 ret = device_probe(dev);
322 printf("Failed to probe keyboard '%s'\n",
326 #ifdef CONFIG_SYS_I2C
329 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
331 * If the console setting is not in environment variables then
332 * console_init_r() will not be calling iomux_doenv() (which
333 * calls console_search_dev()). So we will not dynamically add
334 * devices by calling stdio_probe_device().
336 * So just probe all video devices now so that whichever one is
337 * required will be available.
339 struct udevice *vdev;
342 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
343 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
345 ret = uclass_next_device(&vdev))
348 printf("%s: Video device failed (ret=%d)\n",
351 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
352 IS_ENABLED(CONFIG_CMD_BMP))
355 if (IS_ENABLED(CONFIG_LCD))
357 if (IS_ENABLED(CONFIG_VIDEO) ||
358 IS_ENABLED(CONFIG_CFB_CONSOLE) ||
359 IS_ENABLED(CONFIG_VIDEO_VCXK))
363 #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
368 #ifdef CONFIG_USB_TTY
371 if (IS_ENABLED(CONFIG_NETCONSOLE))
373 #ifdef CONFIG_JTAG_CONSOLE
374 drv_jtag_console_init();
376 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))