blob: a61220ce4b9c606419534cf2178109fbbc138f20 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk91d32562002-09-18 21:21:13 +00002/*
Heiko Schochere0e55bc2012-01-16 21:12:24 +00003 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
4 *
5 * Changes for multibus/multiadapter I2C support.
6 *
wdenk91d32562002-09-18 21:21:13 +00007 * (C) Copyright 2000
8 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
wdenk91d32562002-09-18 21:21:13 +00009 */
10
11#include <config.h>
Simon Glassd3d40352015-10-18 21:17:15 -060012#include <dm.h>
Simon Glass5d609ce2014-07-23 06:55:05 -060013#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
wdenk91d32562002-09-18 21:21:13 +000015#include <stdarg.h>
16#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020017#include <stdio_dev.h>
wdenk7ac16102004-08-01 22:48:16 +000018#include <serial.h>
Igor Opaniuk1f6bfad2019-05-29 09:01:43 +000019#include <splash.h>
wdenk91d32562002-09-18 21:21:13 +000020#include <i2c.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glassd3d40352015-10-18 21:17:15 -060022#include <dm/device-internal.h>
23
Wolfgang Denk6405a152006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020026static struct stdio_dev devs;
27struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
wdenk91d32562002-09-18 21:21:13 +000028char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
29
Andy Shevchenko94331022021-02-11 17:09:36 +020030int stdio_file_to_flags(const int file)
31{
32 switch (file) {
33 case stdin:
34 return DEV_FLAGS_INPUT;
35 case stdout:
36 case stderr:
37 return DEV_FLAGS_OUTPUT;
38 default:
39 return -EINVAL;
40 }
41}
42
Andy Shevchenko2066f392021-02-11 17:09:35 +020043#if CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)
Jeroen Hofstee169eda62014-10-08 22:57:44 +020044static void nulldev_putc(struct stdio_dev *dev, const char c)
wdenk91d32562002-09-18 21:21:13 +000045{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020046 /* nulldev is empty! */
wdenk91d32562002-09-18 21:21:13 +000047}
48
Jeroen Hofstee169eda62014-10-08 22:57:44 +020049static void nulldev_puts(struct stdio_dev *dev, const char *s)
wdenk91d32562002-09-18 21:21:13 +000050{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020051 /* nulldev is empty! */
wdenk91d32562002-09-18 21:21:13 +000052}
53
Jeroen Hofstee169eda62014-10-08 22:57:44 +020054static int nulldev_input(struct stdio_dev *dev)
wdenk91d32562002-09-18 21:21:13 +000055{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020056 /* nulldev is empty! */
57 return 0;
wdenk91d32562002-09-18 21:21:13 +000058}
wdenk91d32562002-09-18 21:21:13 +000059
Andy Shevchenko2066f392021-02-11 17:09:35 +020060static void nulldev_register(void)
61{
62 struct stdio_dev dev;
63
64 memset(&dev, '\0', sizeof(dev));
65
66 strcpy(dev.name, "nulldev");
67 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
68 dev.putc = nulldev_putc;
69 dev.puts = nulldev_puts;
70 dev.getc = nulldev_input;
71 dev.tstc = nulldev_input;
72
73 stdio_register(&dev);
74}
75#else
76static inline void nulldev_register(void) {}
77#endif /* SYS_DEVICE_NULLDEV */
78
Jeroen Hofstee169eda62014-10-08 22:57:44 +020079static void stdio_serial_putc(struct stdio_dev *dev, const char c)
Simon Glass0d1e1f72014-07-23 06:54:59 -060080{
81 serial_putc(c);
82}
83
Jeroen Hofstee169eda62014-10-08 22:57:44 +020084static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
Simon Glass0d1e1f72014-07-23 06:54:59 -060085{
86 serial_puts(s);
87}
88
Pali Rohára48a24f2022-09-05 11:31:19 +020089#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
90static void stdio_serial_flush(struct stdio_dev *dev)
91{
92 serial_flush();
93}
94#endif
95
Jeroen Hofstee169eda62014-10-08 22:57:44 +020096static int stdio_serial_getc(struct stdio_dev *dev)
Simon Glass0d1e1f72014-07-23 06:54:59 -060097{
98 return serial_getc();
99}
100
Jeroen Hofstee169eda62014-10-08 22:57:44 +0200101static int stdio_serial_tstc(struct stdio_dev *dev)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600102{
103 return serial_tstc();
104}
105
wdenk91d32562002-09-18 21:21:13 +0000106/**************************************************************************
107 * SYSTEM DRIVERS
108 **************************************************************************
109 */
110
111static void drv_system_init (void)
112{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200113 struct stdio_dev dev;
wdenk91d32562002-09-18 21:21:13 +0000114
115 memset (&dev, 0, sizeof (dev));
116
117 strcpy (dev.name, "serial");
Bin Meng6abe4b62015-11-03 23:23:37 -0800118 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
Simon Glass0d1e1f72014-07-23 06:54:59 -0600119 dev.putc = stdio_serial_putc;
120 dev.puts = stdio_serial_puts;
Pali Rohára48a24f2022-09-05 11:31:19 +0200121 STDIO_DEV_ASSIGN_FLUSH(&dev, stdio_serial_flush);
Simon Glass0d1e1f72014-07-23 06:54:59 -0600122 dev.getc = stdio_serial_getc;
123 dev.tstc = stdio_serial_tstc;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200124 stdio_register (&dev);
wdenk91d32562002-09-18 21:21:13 +0000125
Andy Shevchenko2066f392021-02-11 17:09:35 +0200126 nulldev_register();
wdenk91d32562002-09-18 21:21:13 +0000127}
128
129/**************************************************************************
130 * DEVICES
131 **************************************************************************
132 */
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200133struct list_head* stdio_get_list(void)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200134{
Simon Glass4b1e3092020-08-11 11:23:40 -0600135 return &devs.list;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200136}
137
Simon Glass4af407f2016-10-05 20:42:16 -0600138/**
139 * stdio_probe_device() - Find a device which provides the given stdio device
140 *
141 * This looks for a device of the given uclass which provides a particular
142 * stdio device. It is currently really only useful for UCLASS_VIDEO.
143 *
144 * Ultimately we want to be able to probe a device by its stdio name. At
145 * present devices register in their probe function (for video devices this
146 * is done in vidconsole_post_probe()) and we don't know what name they will
147 * use until they do so.
148 * TODO(sjg@chromium.org): We should be able to determine the name before
149 * probing, and probe the required device.
150 *
151 * @name: stdio device name (e.g. "vidconsole")
152 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
153 * @sdevp: Returns stdout device, if found, else NULL
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100154 * Return: 0 if found, -ENOENT if no device found with that name, other -ve
Simon Glass4af407f2016-10-05 20:42:16 -0600155 * on other error
156 */
157static int stdio_probe_device(const char *name, enum uclass_id id,
158 struct stdio_dev **sdevp)
159{
160 struct stdio_dev *sdev;
161 struct udevice *dev;
162 int seq, ret;
163
164 *sdevp = NULL;
165 seq = trailing_strtoln(name, NULL);
166 if (seq == -1)
Simon Glass927e6312016-11-13 14:22:00 -0700167 seq = 0;
168 ret = uclass_get_device_by_seq(id, seq, &dev);
169 if (ret == -ENODEV)
Simon Glass4af407f2016-10-05 20:42:16 -0600170 ret = uclass_first_device_err(id, &dev);
Simon Glass4af407f2016-10-05 20:42:16 -0600171 if (ret) {
172 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
173 seq, name);
174 return ret;
175 }
176 /* The device should be be the last one registered */
177 sdev = list_empty(&devs.list) ? NULL :
178 list_last_entry(&devs.list, struct stdio_dev, list);
179 if (!sdev || strcmp(sdev->name, name)) {
180 debug("Device '%s' did not register with stdio as '%s'\n",
181 dev->name, name);
182 return -ENOENT;
183 }
184 *sdevp = sdev;
185
186 return 0;
187}
Simon Glass4af407f2016-10-05 20:42:16 -0600188
Simon Glass913d7612016-11-13 14:21:59 -0700189struct stdio_dev *stdio_get_by_name(const char *name)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200190{
191 struct list_head *pos;
Simon Glass4af407f2016-10-05 20:42:16 -0600192 struct stdio_dev *sdev;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200193
Simon Glass913d7612016-11-13 14:21:59 -0700194 if (!name)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200195 return NULL;
196
Simon Glass4b1e3092020-08-11 11:23:40 -0600197 list_for_each(pos, &devs.list) {
Simon Glass4af407f2016-10-05 20:42:16 -0600198 sdev = list_entry(pos, struct stdio_dev, list);
199 if (strcmp(sdev->name, name) == 0)
200 return sdev;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200201 }
Simon Glass52cb5042022-10-18 07:46:31 -0600202 if (IS_ENABLED(CONFIG_VIDEO)) {
Simon Glass5a36d822020-08-11 11:23:39 -0600203 /*
204 * We did not find a suitable stdio device. If there is a video
205 * driver with a name starting with 'vidconsole', we can try
206 * probing that in the hope that it will produce the required
207 * stdio device.
208 *
209 * This function is sometimes called with the entire value of
210 * 'stdout', which may include a list of devices separate by
211 * commas. Obviously this is not going to work, so we ignore
212 * that case. The call path in that case is
Andy Shevchenkocfaed7f2020-12-21 14:30:03 +0200213 * console_init_r() -> console_search_dev() -> stdio_get_by_name()
Simon Glass5a36d822020-08-11 11:23:39 -0600214 */
215 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
216 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
217 return sdev;
218 }
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200219
220 return NULL;
221}
222
Simon Glass5b4c1222020-08-11 11:23:41 -0600223struct stdio_dev *stdio_clone(struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200224{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200225 struct stdio_dev *_dev;
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200226
Simon Glass5b4c1222020-08-11 11:23:41 -0600227 if (!dev)
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200228 return NULL;
229
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200230 _dev = calloc(1, sizeof(struct stdio_dev));
Simon Glass5b4c1222020-08-11 11:23:41 -0600231 if (!_dev)
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200232 return NULL;
233
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200234 memcpy(_dev, dev, sizeof(struct stdio_dev));
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200235
236 return _dev;
237}
wdenk91d32562002-09-18 21:21:13 +0000238
Simon Glass5d609ce2014-07-23 06:55:05 -0600239int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
wdenk91d32562002-09-18 21:21:13 +0000240{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200241 struct stdio_dev *_dev;
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200242
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200243 _dev = stdio_clone(dev);
Simon Glass5b4c1222020-08-11 11:23:41 -0600244 if (!_dev)
Simon Glass5d609ce2014-07-23 06:55:05 -0600245 return -ENODEV;
Simon Glass4b1e3092020-08-11 11:23:40 -0600246 list_add_tail(&_dev->list, &devs.list);
Simon Glass5d609ce2014-07-23 06:55:05 -0600247 if (devp)
248 *devp = _dev;
249
wdenk91d32562002-09-18 21:21:13 +0000250 return 0;
251}
252
Simon Glass5d609ce2014-07-23 06:55:05 -0600253int stdio_register(struct stdio_dev *dev)
254{
255 return stdio_register_dev(dev, NULL);
256}
257
Hans de Goede1a586382014-09-20 16:54:37 +0200258int stdio_deregister_dev(struct stdio_dev *dev, int force)
wdenk91d32562002-09-18 21:21:13 +0000259{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200260 struct list_head *pos;
Heinrich Schuchardtf5705652023-09-29 02:47:17 +0200261 char temp_names[3][STDIO_NAME_LEN];
Simon Glass5b4c1222020-08-11 11:23:41 -0600262 int i;
wdenk91d32562002-09-18 21:21:13 +0000263
wdenk91d32562002-09-18 21:21:13 +0000264 /* get stdio devices (ListRemoveItem changes the dev list) */
Simon Glass5b4c1222020-08-11 11:23:41 -0600265 for (i = 0 ; i < MAX_FILES; i++) {
266 if (stdio_devices[i] == dev) {
Hans de Goede1a586382014-09-20 16:54:37 +0200267 if (force) {
Simon Glass5b4c1222020-08-11 11:23:41 -0600268 strcpy(temp_names[i], "nulldev");
Hans de Goede1a586382014-09-20 16:54:37 +0200269 continue;
270 }
wdenk91d32562002-09-18 21:21:13 +0000271 /* Device is assigned -> report error */
Simon Glass5b4c1222020-08-11 11:23:41 -0600272 return -EBUSY;
wdenk91d32562002-09-18 21:21:13 +0000273 }
Heinrich Schuchardtf5705652023-09-29 02:47:17 +0200274 strlcpy(&temp_names[i][0], stdio_devices[i]->name,
275 sizeof(temp_names[i]));
wdenk91d32562002-09-18 21:21:13 +0000276 }
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200277
Simon Glass4b1e3092020-08-11 11:23:40 -0600278 list_del(&dev->list);
Hans de Goede35e116c2014-09-24 14:06:09 +0200279 free(dev);
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200280
Simon Glass5b4c1222020-08-11 11:23:41 -0600281 /* reassign device list */
Simon Glass4b1e3092020-08-11 11:23:40 -0600282 list_for_each(pos, &devs.list) {
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200283 dev = list_entry(pos, struct stdio_dev, list);
Simon Glass5b4c1222020-08-11 11:23:41 -0600284 for (i = 0 ; i < MAX_FILES; i++) {
285 if (strcmp(dev->name, temp_names[i]) == 0)
286 stdio_devices[i] = dev;
wdenk91d32562002-09-18 21:21:13 +0000287 }
288 }
Simon Glass5b4c1222020-08-11 11:23:41 -0600289
wdenk91d32562002-09-18 21:21:13 +0000290 return 0;
291}
Simon Glass5d609ce2014-07-23 06:55:05 -0600292
Simon Glass712da5e2014-09-03 17:37:01 -0600293int stdio_init_tables(void)
wdenk91d32562002-09-18 21:21:13 +0000294{
wdenk91d32562002-09-18 21:21:13 +0000295 /* Initialize the list */
Simon Glass4b1e3092020-08-11 11:23:40 -0600296 INIT_LIST_HEAD(&devs.list);
wdenk91d32562002-09-18 21:21:13 +0000297
Simon Glass712da5e2014-09-03 17:37:01 -0600298 return 0;
299}
300
301int stdio_add_devices(void)
302{
Simon Glassd3d40352015-10-18 21:17:15 -0600303 struct udevice *dev;
Simon Glassd3d40352015-10-18 21:17:15 -0600304 int ret;
305
Simon Glass5a36d822020-08-11 11:23:39 -0600306 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
307 /*
308 * For now we probe all the devices here. At some point this
309 * should be done only when the devices are required - e.g. we
310 * have a list of input devices to start up in the stdin
311 * environment variable. That work probably makes more sense
312 * when stdio itself is converted to driver model.
Simon Glass5a36d822020-08-11 11:23:39 -0600313 */
Simon Glass5a36d822020-08-11 11:23:39 -0600314
315 /*
316 * Don't report errors to the caller - assume that they are
317 * non-fatal
318 */
Michal Suchanek73ba6762022-10-12 21:57:55 +0200319 for (ret = uclass_first_device_check(UCLASS_KEYBOARD, &dev);
320 dev;
321 ret = uclass_next_device_check(&dev)) {
Simon Glass5a36d822020-08-11 11:23:39 -0600322 if (ret)
Michal Suchanek73ba6762022-10-12 21:57:55 +0200323 printf("%s: Failed to probe keyboard '%s' (ret=%d)\n",
324 __func__, dev->name, ret);
Simon Glass5a36d822020-08-11 11:23:39 -0600325 }
Simon Glassd3d40352015-10-18 21:17:15 -0600326 }
Tom Rini52b2e262021-08-18 23:12:24 -0400327#if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000328 i2c_init_all();
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000329#endif
Simon Glass52cb5042022-10-18 07:46:31 -0600330 if (IS_ENABLED(CONFIG_VIDEO)) {
Simon Glass5a36d822020-08-11 11:23:39 -0600331 /*
332 * If the console setting is not in environment variables then
333 * console_init_r() will not be calling iomux_doenv() (which
Andy Shevchenkocfaed7f2020-12-21 14:30:03 +0200334 * calls console_search_dev()). So we will not dynamically add
Simon Glass5a36d822020-08-11 11:23:39 -0600335 * devices by calling stdio_probe_device().
336 *
337 * So just probe all video devices now so that whichever one is
338 * required will be available.
339 */
340 struct udevice *vdev;
341 int ret;
342
343 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
Michal Suchanek73ba6762022-10-12 21:57:55 +0200344 for (ret = uclass_first_device_check(UCLASS_VIDEO,
345 &vdev);
346 vdev;
347 ret = uclass_next_device_check(&vdev)) {
348 if (ret)
349 printf("%s: Failed to probe video device '%s' (ret=%d)\n",
350 __func__, vdev->name, ret);
351 }
Simon Glass5a36d822020-08-11 11:23:39 -0600352 }
353 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
354 IS_ENABLED(CONFIG_CMD_BMP))
355 splash_display();
Simon Glass5a36d822020-08-11 11:23:39 -0600356 }
Simon Glass0005cb22016-01-18 19:52:23 -0700357
Simon Glass5a36d822020-08-11 11:23:39 -0600358 drv_system_init();
359 serial_stdio_init();
wdenk29e7f5a2004-03-12 00:14:09 +0000360#ifdef CONFIG_USB_TTY
Simon Glass5a36d822020-08-11 11:23:39 -0600361 drv_usbtty_init();
wdenk29e7f5a2004-03-12 00:14:09 +0000362#endif
Loic Poulaineb78f832021-11-25 18:16:15 +0100363#ifdef CONFIG_USB_FUNCTION_ACM
364 drv_usbacm_init ();
365#endif
Simon Glass5a36d822020-08-11 11:23:39 -0600366 if (IS_ENABLED(CONFIG_NETCONSOLE))
367 drv_nc_init();
Mike Frysinger7504e972008-10-11 21:51:20 -0400368#ifdef CONFIG_JTAG_CONSOLE
Simon Glass5a36d822020-08-11 11:23:39 -0600369 drv_jtag_console_init();
Vadim Bendebury263a8bd2012-10-12 18:48:47 +0000370#endif
Simon Glass5a36d822020-08-11 11:23:39 -0600371 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))
372 cbmemc_init();
Simon Glass712da5e2014-09-03 17:37:01 -0600373
374 return 0;
375}