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>
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 static void nulldev_putc(struct stdio_dev *dev, const char c)
33 /* nulldev is empty! */
36 static void nulldev_puts(struct stdio_dev *dev, const char *s)
38 /* nulldev is empty! */
41 static int nulldev_input(struct stdio_dev *dev)
43 /* nulldev is empty! */
47 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
52 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
57 static int stdio_serial_getc(struct stdio_dev *dev)
62 static int stdio_serial_tstc(struct stdio_dev *dev)
67 /**************************************************************************
69 **************************************************************************
72 static void drv_system_init (void)
76 memset (&dev, 0, sizeof (dev));
78 strcpy (dev.name, "serial");
79 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
80 dev.putc = stdio_serial_putc;
81 dev.puts = stdio_serial_puts;
82 dev.getc = stdio_serial_getc;
83 dev.tstc = stdio_serial_tstc;
84 stdio_register (&dev);
86 if (CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)) {
87 memset(&dev, '\0', sizeof(dev));
89 strcpy(dev.name, "nulldev");
90 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
91 dev.putc = nulldev_putc;
92 dev.puts = nulldev_puts;
93 dev.getc = nulldev_input;
94 dev.tstc = nulldev_input;
100 /**************************************************************************
102 **************************************************************************
104 struct list_head* stdio_get_list(void)
110 * stdio_probe_device() - Find a device which provides the given stdio device
112 * This looks for a device of the given uclass which provides a particular
113 * stdio device. It is currently really only useful for UCLASS_VIDEO.
115 * Ultimately we want to be able to probe a device by its stdio name. At
116 * present devices register in their probe function (for video devices this
117 * is done in vidconsole_post_probe()) and we don't know what name they will
118 * use until they do so.
119 * TODO(sjg@chromium.org): We should be able to determine the name before
120 * probing, and probe the required device.
122 * @name: stdio device name (e.g. "vidconsole")
123 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
124 * @sdevp: Returns stdout device, if found, else NULL
125 * @return 0 if found, -ENOENT if no device found with that name, other -ve
128 static int stdio_probe_device(const char *name, enum uclass_id id,
129 struct stdio_dev **sdevp)
131 struct stdio_dev *sdev;
136 seq = trailing_strtoln(name, NULL);
139 ret = uclass_get_device_by_seq(id, seq, &dev);
141 ret = uclass_first_device_err(id, &dev);
143 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
147 /* The device should be be the last one registered */
148 sdev = list_empty(&devs.list) ? NULL :
149 list_last_entry(&devs.list, struct stdio_dev, list);
150 if (!sdev || strcmp(sdev->name, name)) {
151 debug("Device '%s' did not register with stdio as '%s'\n",
160 struct stdio_dev *stdio_get_by_name(const char *name)
162 struct list_head *pos;
163 struct stdio_dev *sdev;
168 list_for_each(pos, &devs.list) {
169 sdev = list_entry(pos, struct stdio_dev, list);
170 if (strcmp(sdev->name, name) == 0)
173 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
175 * We did not find a suitable stdio device. If there is a video
176 * driver with a name starting with 'vidconsole', we can try
177 * probing that in the hope that it will produce the required
180 * This function is sometimes called with the entire value of
181 * 'stdout', which may include a list of devices separate by
182 * commas. Obviously this is not going to work, so we ignore
183 * that case. The call path in that case is
184 * console_init_r() -> search_device() -> stdio_get_by_name()
186 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
187 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
194 struct stdio_dev *stdio_clone(struct stdio_dev *dev)
196 struct stdio_dev *_dev;
201 _dev = calloc(1, sizeof(struct stdio_dev));
205 memcpy(_dev, dev, sizeof(struct stdio_dev));
210 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
212 struct stdio_dev *_dev;
214 _dev = stdio_clone(dev);
217 list_add_tail(&_dev->list, &devs.list);
224 int stdio_register(struct stdio_dev *dev)
226 return stdio_register_dev(dev, NULL);
229 int stdio_deregister_dev(struct stdio_dev *dev, int force)
231 struct list_head *pos;
232 char temp_names[3][16];
235 /* get stdio devices (ListRemoveItem changes the dev list) */
236 for (i = 0 ; i < MAX_FILES; i++) {
237 if (stdio_devices[i] == dev) {
239 strcpy(temp_names[i], "nulldev");
242 /* Device is assigned -> report error */
245 memcpy(&temp_names[i][0], stdio_devices[i]->name,
246 sizeof(temp_names[i]));
249 list_del(&dev->list);
252 /* reassign device list */
253 list_for_each(pos, &devs.list) {
254 dev = list_entry(pos, struct stdio_dev, list);
255 for (i = 0 ; i < MAX_FILES; i++) {
256 if (strcmp(dev->name, temp_names[i]) == 0)
257 stdio_devices[i] = dev;
264 int stdio_deregister(const char *devname, int force)
266 struct stdio_dev *dev;
268 dev = stdio_get_by_name(devname);
269 if (!dev) /* device not found */
272 return stdio_deregister_dev(dev, force);
275 int stdio_init_tables(void)
277 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
278 /* already relocated for current ARM implementation */
279 ulong relocation_offset = gd->reloc_off;
282 /* relocate device name pointers */
283 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
284 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
287 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
289 /* Initialize the list */
290 INIT_LIST_HEAD(&devs.list);
295 int stdio_add_devices(void)
301 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
303 * For now we probe all the devices here. At some point this
304 * should be done only when the devices are required - e.g. we
305 * have a list of input devices to start up in the stdin
306 * environment variable. That work probably makes more sense
307 * when stdio itself is converted to driver model.
309 * TODO(sjg@chromium.org): Convert changing
310 * uclass_first_device() etc. to return the device even on
311 * error. Then we could use that here.
313 ret = uclass_get(UCLASS_KEYBOARD, &uc);
318 * Don't report errors to the caller - assume that they are
321 uclass_foreach_dev(dev, uc) {
322 ret = device_probe(dev);
324 printf("Failed to probe keyboard '%s'\n",
328 #ifdef CONFIG_SYS_I2C
331 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
333 * If the console setting is not in environment variables then
334 * console_init_r() will not be calling iomux_doenv() (which
335 * calls search_device()). So we will not dynamically add
336 * devices by calling stdio_probe_device().
338 * So just probe all video devices now so that whichever one is
339 * required will be available.
341 struct udevice *vdev;
344 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
345 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
347 ret = uclass_next_device(&vdev))
350 printf("%s: Video device failed (ret=%d)\n",
353 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
354 IS_ENABLED(CONFIG_CMD_BMP))
357 if (IS_ENABLED(CONFIG_LCD))
359 if (IS_ENABLED(CONFIG_VIDEO) || IS_ENABLED(CONFIG_CFB_CONSOLE))
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))