blob: a15f30804bf58a5dfa1d507b13b41c2a90942887 [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>
12#include <common.h>
Simon Glassd3d40352015-10-18 21:17:15 -060013#include <dm.h>
Simon Glass5d609ce2014-07-23 06:55:05 -060014#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
wdenk91d32562002-09-18 21:21:13 +000016#include <stdarg.h>
17#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020018#include <stdio_dev.h>
wdenk7ac16102004-08-01 22:48:16 +000019#include <serial.h>
Igor Opaniuk1f6bfad2019-05-29 09:01:43 +000020#include <splash.h>
wdenk91d32562002-09-18 21:21:13 +000021#include <i2c.h>
22
Simon Glassd3d40352015-10-18 21:17:15 -060023#include <dm/device-internal.h>
24
Wolfgang Denk6405a152006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020027static struct stdio_dev devs;
28struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
wdenk91d32562002-09-18 21:21:13 +000029char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
30
Jeroen Hofstee169eda62014-10-08 22:57:44 +020031static void nulldev_putc(struct stdio_dev *dev, const char c)
wdenk91d32562002-09-18 21:21:13 +000032{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020033 /* nulldev is empty! */
wdenk91d32562002-09-18 21:21:13 +000034}
35
Jeroen Hofstee169eda62014-10-08 22:57:44 +020036static void nulldev_puts(struct stdio_dev *dev, const char *s)
wdenk91d32562002-09-18 21:21:13 +000037{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020038 /* nulldev is empty! */
wdenk91d32562002-09-18 21:21:13 +000039}
40
Jeroen Hofstee169eda62014-10-08 22:57:44 +020041static int nulldev_input(struct stdio_dev *dev)
wdenk91d32562002-09-18 21:21:13 +000042{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020043 /* nulldev is empty! */
44 return 0;
wdenk91d32562002-09-18 21:21:13 +000045}
wdenk91d32562002-09-18 21:21:13 +000046
Jeroen Hofstee169eda62014-10-08 22:57:44 +020047static void stdio_serial_putc(struct stdio_dev *dev, const char c)
Simon Glass0d1e1f72014-07-23 06:54:59 -060048{
49 serial_putc(c);
50}
51
Jeroen Hofstee169eda62014-10-08 22:57:44 +020052static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
Simon Glass0d1e1f72014-07-23 06:54:59 -060053{
54 serial_puts(s);
55}
56
Jeroen Hofstee169eda62014-10-08 22:57:44 +020057static int stdio_serial_getc(struct stdio_dev *dev)
Simon Glass0d1e1f72014-07-23 06:54:59 -060058{
59 return serial_getc();
60}
61
Jeroen Hofstee169eda62014-10-08 22:57:44 +020062static int stdio_serial_tstc(struct stdio_dev *dev)
Simon Glass0d1e1f72014-07-23 06:54:59 -060063{
64 return serial_tstc();
65}
66
wdenk91d32562002-09-18 21:21:13 +000067/**************************************************************************
68 * SYSTEM DRIVERS
69 **************************************************************************
70 */
71
72static void drv_system_init (void)
73{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020074 struct stdio_dev dev;
wdenk91d32562002-09-18 21:21:13 +000075
76 memset (&dev, 0, sizeof (dev));
77
78 strcpy (dev.name, "serial");
Bin Meng6abe4b62015-11-03 23:23:37 -080079 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
Simon Glass0d1e1f72014-07-23 06:54:59 -060080 dev.putc = stdio_serial_putc;
81 dev.puts = stdio_serial_puts;
82 dev.getc = stdio_serial_getc;
83 dev.tstc = stdio_serial_tstc;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020084 stdio_register (&dev);
wdenk91d32562002-09-18 21:21:13 +000085
Simon Glassd6336b32020-08-11 11:23:37 -060086 if (CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV)) {
87 memset(&dev, '\0', sizeof(dev));
wdenk91d32562002-09-18 21:21:13 +000088
Simon Glassd6336b32020-08-11 11:23:37 -060089 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;
wdenk91d32562002-09-18 21:21:13 +000095
Simon Glassd6336b32020-08-11 11:23:37 -060096 stdio_register(&dev);
97 }
wdenk91d32562002-09-18 21:21:13 +000098}
99
100/**************************************************************************
101 * DEVICES
102 **************************************************************************
103 */
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200104struct list_head* stdio_get_list(void)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200105{
Simon Glass4b1e3092020-08-11 11:23:40 -0600106 return &devs.list;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200107}
108
Simon Glass4af407f2016-10-05 20:42:16 -0600109/**
110 * stdio_probe_device() - Find a device which provides the given stdio device
111 *
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.
114 *
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.
121 *
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
126 * on other error
127 */
128static int stdio_probe_device(const char *name, enum uclass_id id,
129 struct stdio_dev **sdevp)
130{
131 struct stdio_dev *sdev;
132 struct udevice *dev;
133 int seq, ret;
134
135 *sdevp = NULL;
136 seq = trailing_strtoln(name, NULL);
137 if (seq == -1)
Simon Glass927e6312016-11-13 14:22:00 -0700138 seq = 0;
139 ret = uclass_get_device_by_seq(id, seq, &dev);
140 if (ret == -ENODEV)
Simon Glass4af407f2016-10-05 20:42:16 -0600141 ret = uclass_first_device_err(id, &dev);
Simon Glass4af407f2016-10-05 20:42:16 -0600142 if (ret) {
143 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
144 seq, name);
145 return ret;
146 }
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",
152 dev->name, name);
153 return -ENOENT;
154 }
155 *sdevp = sdev;
156
157 return 0;
158}
Simon Glass4af407f2016-10-05 20:42:16 -0600159
Simon Glass913d7612016-11-13 14:21:59 -0700160struct stdio_dev *stdio_get_by_name(const char *name)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200161{
162 struct list_head *pos;
Simon Glass4af407f2016-10-05 20:42:16 -0600163 struct stdio_dev *sdev;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200164
Simon Glass913d7612016-11-13 14:21:59 -0700165 if (!name)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200166 return NULL;
167
Simon Glass4b1e3092020-08-11 11:23:40 -0600168 list_for_each(pos, &devs.list) {
Simon Glass4af407f2016-10-05 20:42:16 -0600169 sdev = list_entry(pos, struct stdio_dev, list);
170 if (strcmp(sdev->name, name) == 0)
171 return sdev;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200172 }
Simon Glass5a36d822020-08-11 11:23:39 -0600173 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
174 /*
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
178 * stdio device.
179 *
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()
185 */
186 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
187 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
188 return sdev;
189 }
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200190
191 return NULL;
192}
193
Simon Glass5b4c1222020-08-11 11:23:41 -0600194struct stdio_dev *stdio_clone(struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200195{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200196 struct stdio_dev *_dev;
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200197
Simon Glass5b4c1222020-08-11 11:23:41 -0600198 if (!dev)
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200199 return NULL;
200
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200201 _dev = calloc(1, sizeof(struct stdio_dev));
Simon Glass5b4c1222020-08-11 11:23:41 -0600202 if (!_dev)
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200203 return NULL;
204
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200205 memcpy(_dev, dev, sizeof(struct stdio_dev));
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200206
207 return _dev;
208}
wdenk91d32562002-09-18 21:21:13 +0000209
Simon Glass5d609ce2014-07-23 06:55:05 -0600210int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
wdenk91d32562002-09-18 21:21:13 +0000211{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200212 struct stdio_dev *_dev;
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200213
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200214 _dev = stdio_clone(dev);
Simon Glass5b4c1222020-08-11 11:23:41 -0600215 if (!_dev)
Simon Glass5d609ce2014-07-23 06:55:05 -0600216 return -ENODEV;
Simon Glass4b1e3092020-08-11 11:23:40 -0600217 list_add_tail(&_dev->list, &devs.list);
Simon Glass5d609ce2014-07-23 06:55:05 -0600218 if (devp)
219 *devp = _dev;
220
wdenk91d32562002-09-18 21:21:13 +0000221 return 0;
222}
223
Simon Glass5d609ce2014-07-23 06:55:05 -0600224int stdio_register(struct stdio_dev *dev)
225{
226 return stdio_register_dev(dev, NULL);
227}
228
Hans de Goede1a586382014-09-20 16:54:37 +0200229int stdio_deregister_dev(struct stdio_dev *dev, int force)
wdenk91d32562002-09-18 21:21:13 +0000230{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200231 struct list_head *pos;
Bradley Bolen466d7392011-08-22 11:48:05 +0000232 char temp_names[3][16];
Simon Glass5b4c1222020-08-11 11:23:41 -0600233 int i;
wdenk91d32562002-09-18 21:21:13 +0000234
wdenk91d32562002-09-18 21:21:13 +0000235 /* get stdio devices (ListRemoveItem changes the dev list) */
Simon Glass5b4c1222020-08-11 11:23:41 -0600236 for (i = 0 ; i < MAX_FILES; i++) {
237 if (stdio_devices[i] == dev) {
Hans de Goede1a586382014-09-20 16:54:37 +0200238 if (force) {
Simon Glass5b4c1222020-08-11 11:23:41 -0600239 strcpy(temp_names[i], "nulldev");
Hans de Goede1a586382014-09-20 16:54:37 +0200240 continue;
241 }
wdenk91d32562002-09-18 21:21:13 +0000242 /* Device is assigned -> report error */
Simon Glass5b4c1222020-08-11 11:23:41 -0600243 return -EBUSY;
wdenk91d32562002-09-18 21:21:13 +0000244 }
Simon Glass5b4c1222020-08-11 11:23:41 -0600245 memcpy(&temp_names[i][0], stdio_devices[i]->name,
246 sizeof(temp_names[i]));
wdenk91d32562002-09-18 21:21:13 +0000247 }
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200248
Simon Glass4b1e3092020-08-11 11:23:40 -0600249 list_del(&dev->list);
Hans de Goede35e116c2014-09-24 14:06:09 +0200250 free(dev);
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200251
Simon Glass5b4c1222020-08-11 11:23:41 -0600252 /* reassign device list */
Simon Glass4b1e3092020-08-11 11:23:40 -0600253 list_for_each(pos, &devs.list) {
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200254 dev = list_entry(pos, struct stdio_dev, list);
Simon Glass5b4c1222020-08-11 11:23:41 -0600255 for (i = 0 ; i < MAX_FILES; i++) {
256 if (strcmp(dev->name, temp_names[i]) == 0)
257 stdio_devices[i] = dev;
wdenk91d32562002-09-18 21:21:13 +0000258 }
259 }
Simon Glass5b4c1222020-08-11 11:23:41 -0600260
wdenk91d32562002-09-18 21:21:13 +0000261 return 0;
262}
Simon Glass5d609ce2014-07-23 06:55:05 -0600263
Hans de Goede1a586382014-09-20 16:54:37 +0200264int stdio_deregister(const char *devname, int force)
Simon Glass5d609ce2014-07-23 06:55:05 -0600265{
266 struct stdio_dev *dev;
267
268 dev = stdio_get_by_name(devname);
Simon Glass5d609ce2014-07-23 06:55:05 -0600269 if (!dev) /* device not found */
270 return -ENODEV;
271
Hans de Goede1a586382014-09-20 16:54:37 +0200272 return stdio_deregister_dev(dev, force);
Simon Glass5d609ce2014-07-23 06:55:05 -0600273}
wdenk91d32562002-09-18 21:21:13 +0000274
Simon Glass712da5e2014-09-03 17:37:01 -0600275int stdio_init_tables(void)
wdenk91d32562002-09-18 21:21:13 +0000276{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200277#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500278 /* already relocated for current ARM implementation */
wdenk91d32562002-09-18 21:21:13 +0000279 ulong relocation_offset = gd->reloc_off;
wdenk56958612003-06-22 17:18:28 +0000280 int i;
wdenk91d32562002-09-18 21:21:13 +0000281
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]) +
285 relocation_offset);
286 }
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200287#endif /* CONFIG_NEEDS_MANUAL_RELOC */
wdenk91d32562002-09-18 21:21:13 +0000288
289 /* Initialize the list */
Simon Glass4b1e3092020-08-11 11:23:40 -0600290 INIT_LIST_HEAD(&devs.list);
wdenk91d32562002-09-18 21:21:13 +0000291
Simon Glass712da5e2014-09-03 17:37:01 -0600292 return 0;
293}
294
295int stdio_add_devices(void)
296{
Simon Glassd3d40352015-10-18 21:17:15 -0600297 struct udevice *dev;
298 struct uclass *uc;
299 int ret;
300
Simon Glass5a36d822020-08-11 11:23:39 -0600301 if (IS_ENABLED(CONFIG_DM_KEYBOARD)) {
302 /*
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.
308 *
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.
312 */
313 ret = uclass_get(UCLASS_KEYBOARD, &uc);
Simon Glassd3d40352015-10-18 21:17:15 -0600314 if (ret)
Simon Glass5a36d822020-08-11 11:23:39 -0600315 return ret;
316
317 /*
318 * Don't report errors to the caller - assume that they are
319 * non-fatal
320 */
321 uclass_foreach_dev(dev, uc) {
322 ret = device_probe(dev);
323 if (ret)
324 printf("Failed to probe keyboard '%s'\n",
325 dev->name);
326 }
Simon Glassd3d40352015-10-18 21:17:15 -0600327 }
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000328#ifdef CONFIG_SYS_I2C
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000329 i2c_init_all();
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000330#endif
Simon Glass5a36d822020-08-11 11:23:39 -0600331 if (IS_ENABLED(CONFIG_DM_VIDEO)) {
332 /*
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().
337 *
338 * So just probe all video devices now so that whichever one is
339 * required will be available.
340 */
341 struct udevice *vdev;
342 int ret;
343
344 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_IS_IN_ENV)) {
345 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
346 vdev;
347 ret = uclass_next_device(&vdev))
348 ;
349 if (ret)
350 printf("%s: Video device failed (ret=%d)\n",
351 __func__, ret);
352 }
353 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
354 IS_ENABLED(CONFIG_CMD_BMP))
355 splash_display();
356 } else {
357 if (IS_ENABLED(CONFIG_LCD))
358 drv_lcd_init();
Anatolij Gustschin3d471a12020-10-18 20:32:35 +0200359 if (IS_ENABLED(CONFIG_VIDEO) ||
360 IS_ENABLED(CONFIG_CFB_CONSOLE) ||
361 IS_ENABLED(CONFIG_VIDEO_VCXK))
Simon Glass5a36d822020-08-11 11:23:39 -0600362 drv_video_init();
363 }
Simon Glass0005cb22016-01-18 19:52:23 -0700364
Simon Glassd3d40352015-10-18 21:17:15 -0600365#if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
Simon Glass5a36d822020-08-11 11:23:39 -0600366 drv_keyboard_init();
wdenk91d32562002-09-18 21:21:13 +0000367#endif
Simon Glass5a36d822020-08-11 11:23:39 -0600368 drv_system_init();
369 serial_stdio_init();
wdenk29e7f5a2004-03-12 00:14:09 +0000370#ifdef CONFIG_USB_TTY
Simon Glass5a36d822020-08-11 11:23:39 -0600371 drv_usbtty_init();
wdenk29e7f5a2004-03-12 00:14:09 +0000372#endif
Simon Glass5a36d822020-08-11 11:23:39 -0600373 if (IS_ENABLED(CONFIG_NETCONSOLE))
374 drv_nc_init();
Mike Frysinger7504e972008-10-11 21:51:20 -0400375#ifdef CONFIG_JTAG_CONSOLE
Simon Glass5a36d822020-08-11 11:23:39 -0600376 drv_jtag_console_init();
Vadim Bendebury263a8bd2012-10-12 18:48:47 +0000377#endif
Simon Glass5a36d822020-08-11 11:23:39 -0600378 if (IS_ENABLED(CONFIG_CBMEM_CONSOLE))
379 cbmemc_init();
Simon Glass712da5e2014-09-03 17:37:01 -0600380
381 return 0;
382}
383
384int stdio_init(void)
385{
386 stdio_init_tables();
387 stdio_add_devices();
388
389 return 0;
wdenk91d32562002-09-18 21:21:13 +0000390}