blob: ee22c1fd58523aa407bb7d1e63a9262e1a342306 [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>
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>
Heiko Schocher479a4cf2013-01-29 08:53:15 +010019
Simon Glass1a46a722017-05-12 21:09:56 -060020#if defined(CONFIG_SYS_I2C)
wdenk91d32562002-09-18 21:21:13 +000021#include <i2c.h>
wdenk1272e232002-11-10 22:06:23 +000022#endif
wdenk91d32562002-09-18 21:21:13 +000023
Simon Glassd3d40352015-10-18 21:17:15 -060024#include <dm/device-internal.h>
25
Wolfgang Denk6405a152006-03-31 18:32:53 +020026DECLARE_GLOBAL_DATA_PTR;
27
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020028static struct stdio_dev devs;
29struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
wdenk91d32562002-09-18 21:21:13 +000030char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
31
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020032#if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
33#define CONFIG_SYS_DEVICE_NULLDEV 1
wdenk92bbe3f2003-04-20 14:04:18 +000034#endif
35
Simon Glass3110ece2016-10-17 20:13:02 -060036#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
Hans de Goede1a586382014-09-20 16:54:37 +020037#define CONFIG_SYS_DEVICE_NULLDEV 1
38#endif
wdenk92bbe3f2003-04-20 14:04:18 +000039
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020040#ifdef CONFIG_SYS_DEVICE_NULLDEV
Jeroen Hofstee169eda62014-10-08 22:57:44 +020041static void nulldev_putc(struct stdio_dev *dev, const char c)
wdenk91d32562002-09-18 21:21:13 +000042{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020043 /* nulldev is empty! */
wdenk91d32562002-09-18 21:21:13 +000044}
45
Jeroen Hofstee169eda62014-10-08 22:57:44 +020046static void nulldev_puts(struct stdio_dev *dev, const char *s)
wdenk91d32562002-09-18 21:21:13 +000047{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020048 /* nulldev is empty! */
wdenk91d32562002-09-18 21:21:13 +000049}
50
Jeroen Hofstee169eda62014-10-08 22:57:44 +020051static int nulldev_input(struct stdio_dev *dev)
wdenk91d32562002-09-18 21:21:13 +000052{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020053 /* nulldev is empty! */
54 return 0;
wdenk91d32562002-09-18 21:21:13 +000055}
56#endif
57
Jeroen Hofstee169eda62014-10-08 22:57:44 +020058static void stdio_serial_putc(struct stdio_dev *dev, const char c)
Simon Glass0d1e1f72014-07-23 06:54:59 -060059{
60 serial_putc(c);
61}
62
Jeroen Hofstee169eda62014-10-08 22:57:44 +020063static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
Simon Glass0d1e1f72014-07-23 06:54:59 -060064{
65 serial_puts(s);
66}
67
Jeroen Hofstee169eda62014-10-08 22:57:44 +020068static int stdio_serial_getc(struct stdio_dev *dev)
Simon Glass0d1e1f72014-07-23 06:54:59 -060069{
70 return serial_getc();
71}
72
Jeroen Hofstee169eda62014-10-08 22:57:44 +020073static int stdio_serial_tstc(struct stdio_dev *dev)
Simon Glass0d1e1f72014-07-23 06:54:59 -060074{
75 return serial_tstc();
76}
77
wdenk91d32562002-09-18 21:21:13 +000078/**************************************************************************
79 * SYSTEM DRIVERS
80 **************************************************************************
81 */
82
83static void drv_system_init (void)
84{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020085 struct stdio_dev dev;
wdenk91d32562002-09-18 21:21:13 +000086
87 memset (&dev, 0, sizeof (dev));
88
89 strcpy (dev.name, "serial");
Bin Meng6abe4b62015-11-03 23:23:37 -080090 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
Simon Glass0d1e1f72014-07-23 06:54:59 -060091 dev.putc = stdio_serial_putc;
92 dev.puts = stdio_serial_puts;
93 dev.getc = stdio_serial_getc;
94 dev.tstc = stdio_serial_tstc;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020095 stdio_register (&dev);
wdenk91d32562002-09-18 21:21:13 +000096
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020097#ifdef CONFIG_SYS_DEVICE_NULLDEV
wdenk91d32562002-09-18 21:21:13 +000098 memset (&dev, 0, sizeof (dev));
99
100 strcpy (dev.name, "nulldev");
Bin Meng6abe4b62015-11-03 23:23:37 -0800101 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
wdenk91d32562002-09-18 21:21:13 +0000102 dev.putc = nulldev_putc;
103 dev.puts = nulldev_puts;
104 dev.getc = nulldev_input;
105 dev.tstc = nulldev_input;
106
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200107 stdio_register (&dev);
wdenk91d32562002-09-18 21:21:13 +0000108#endif
109}
110
111/**************************************************************************
112 * DEVICES
113 **************************************************************************
114 */
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200115struct list_head* stdio_get_list(void)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200116{
117 return &(devs.list);
118}
119
Simon Glass4af407f2016-10-05 20:42:16 -0600120#ifdef CONFIG_DM_VIDEO
121/**
122 * stdio_probe_device() - Find a device which provides the given stdio device
123 *
124 * This looks for a device of the given uclass which provides a particular
125 * stdio device. It is currently really only useful for UCLASS_VIDEO.
126 *
127 * Ultimately we want to be able to probe a device by its stdio name. At
128 * present devices register in their probe function (for video devices this
129 * is done in vidconsole_post_probe()) and we don't know what name they will
130 * use until they do so.
131 * TODO(sjg@chromium.org): We should be able to determine the name before
132 * probing, and probe the required device.
133 *
134 * @name: stdio device name (e.g. "vidconsole")
135 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
136 * @sdevp: Returns stdout device, if found, else NULL
137 * @return 0 if found, -ENOENT if no device found with that name, other -ve
138 * on other error
139 */
140static int stdio_probe_device(const char *name, enum uclass_id id,
141 struct stdio_dev **sdevp)
142{
143 struct stdio_dev *sdev;
144 struct udevice *dev;
145 int seq, ret;
146
147 *sdevp = NULL;
148 seq = trailing_strtoln(name, NULL);
149 if (seq == -1)
Simon Glass927e6312016-11-13 14:22:00 -0700150 seq = 0;
151 ret = uclass_get_device_by_seq(id, seq, &dev);
152 if (ret == -ENODEV)
Simon Glass4af407f2016-10-05 20:42:16 -0600153 ret = uclass_first_device_err(id, &dev);
Simon Glass4af407f2016-10-05 20:42:16 -0600154 if (ret) {
155 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
156 seq, name);
157 return ret;
158 }
159 /* The device should be be the last one registered */
160 sdev = list_empty(&devs.list) ? NULL :
161 list_last_entry(&devs.list, struct stdio_dev, list);
162 if (!sdev || strcmp(sdev->name, name)) {
163 debug("Device '%s' did not register with stdio as '%s'\n",
164 dev->name, name);
165 return -ENOENT;
166 }
167 *sdevp = sdev;
168
169 return 0;
170}
171#endif
172
Simon Glass913d7612016-11-13 14:21:59 -0700173struct stdio_dev *stdio_get_by_name(const char *name)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200174{
175 struct list_head *pos;
Simon Glass4af407f2016-10-05 20:42:16 -0600176 struct stdio_dev *sdev;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200177
Simon Glass913d7612016-11-13 14:21:59 -0700178 if (!name)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200179 return NULL;
180
181 list_for_each(pos, &(devs.list)) {
Simon Glass4af407f2016-10-05 20:42:16 -0600182 sdev = list_entry(pos, struct stdio_dev, list);
183 if (strcmp(sdev->name, name) == 0)
184 return sdev;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200185 }
Simon Glass4af407f2016-10-05 20:42:16 -0600186#ifdef CONFIG_DM_VIDEO
187 /*
188 * We did not find a suitable stdio device. If there is a video
189 * driver with a name starting with 'vidconsole', we can try probing
190 * that in the hope that it will produce the required stdio device.
191 *
192 * This function is sometimes called with the entire value of
193 * 'stdout', which may include a list of devices separate by commas.
194 * Obviously this is not going to work, so we ignore that case. The
195 * call path in that case is console_init_r() -> search_device() ->
196 * stdio_get_by_name().
197 */
198 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
199 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
200 return sdev;
201#endif
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200202
203 return NULL;
204}
205
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200206struct stdio_dev* stdio_clone(struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200207{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200208 struct stdio_dev *_dev;
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200209
210 if(!dev)
211 return NULL;
212
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200213 _dev = calloc(1, sizeof(struct stdio_dev));
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200214
215 if(!_dev)
216 return NULL;
217
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200218 memcpy(_dev, dev, sizeof(struct stdio_dev));
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200219
220 return _dev;
221}
wdenk91d32562002-09-18 21:21:13 +0000222
Simon Glass5d609ce2014-07-23 06:55:05 -0600223int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
wdenk91d32562002-09-18 21:21:13 +0000224{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200225 struct stdio_dev *_dev;
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200226
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200227 _dev = stdio_clone(dev);
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200228 if(!_dev)
Simon Glass5d609ce2014-07-23 06:55:05 -0600229 return -ENODEV;
Stefan Roesec3be13b2008-09-05 10:47:46 +0200230 list_add_tail(&(_dev->list), &(devs.list));
Simon Glass5d609ce2014-07-23 06:55:05 -0600231 if (devp)
232 *devp = _dev;
233
wdenk91d32562002-09-18 21:21:13 +0000234 return 0;
235}
236
Simon Glass5d609ce2014-07-23 06:55:05 -0600237int stdio_register(struct stdio_dev *dev)
238{
239 return stdio_register_dev(dev, NULL);
240}
241
wdenk91d32562002-09-18 21:21:13 +0000242/* deregister the device "devname".
243 * returns 0 if success, -1 if device is assigned and 1 if devname not found
244 */
Simon Glass3110ece2016-10-17 20:13:02 -0600245#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
Hans de Goede1a586382014-09-20 16:54:37 +0200246int stdio_deregister_dev(struct stdio_dev *dev, int force)
wdenk91d32562002-09-18 21:21:13 +0000247{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200248 int l;
249 struct list_head *pos;
Bradley Bolen466d7392011-08-22 11:48:05 +0000250 char temp_names[3][16];
wdenk91d32562002-09-18 21:21:13 +0000251
wdenk91d32562002-09-18 21:21:13 +0000252 /* get stdio devices (ListRemoveItem changes the dev list) */
253 for (l=0 ; l< MAX_FILES; l++) {
254 if (stdio_devices[l] == dev) {
Hans de Goede1a586382014-09-20 16:54:37 +0200255 if (force) {
256 strcpy(temp_names[l], "nulldev");
257 continue;
258 }
wdenk91d32562002-09-18 21:21:13 +0000259 /* Device is assigned -> report error */
260 return -1;
261 }
262 memcpy (&temp_names[l][0],
263 stdio_devices[l]->name,
Bradley Bolen466d7392011-08-22 11:48:05 +0000264 sizeof(temp_names[l]));
wdenk91d32562002-09-18 21:21:13 +0000265 }
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200266
267 list_del(&(dev->list));
Hans de Goede35e116c2014-09-24 14:06:09 +0200268 free(dev);
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200269
wdenk91d32562002-09-18 21:21:13 +0000270 /* reassign Device list */
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200271 list_for_each(pos, &(devs.list)) {
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200272 dev = list_entry(pos, struct stdio_dev, list);
wdenk91d32562002-09-18 21:21:13 +0000273 for (l=0 ; l< MAX_FILES; l++) {
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200274 if(strcmp(dev->name, temp_names[l]) == 0)
wdenk91d32562002-09-18 21:21:13 +0000275 stdio_devices[l] = dev;
wdenk91d32562002-09-18 21:21:13 +0000276 }
277 }
278 return 0;
279}
Simon Glass5d609ce2014-07-23 06:55:05 -0600280
Hans de Goede1a586382014-09-20 16:54:37 +0200281int stdio_deregister(const char *devname, int force)
Simon Glass5d609ce2014-07-23 06:55:05 -0600282{
283 struct stdio_dev *dev;
284
285 dev = stdio_get_by_name(devname);
286
287 if (!dev) /* device not found */
288 return -ENODEV;
289
Hans de Goede1a586382014-09-20 16:54:37 +0200290 return stdio_deregister_dev(dev, force);
Simon Glass5d609ce2014-07-23 06:55:05 -0600291}
Simon Glass3110ece2016-10-17 20:13:02 -0600292#endif /* CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) */
wdenk91d32562002-09-18 21:21:13 +0000293
Simon Glass712da5e2014-09-03 17:37:01 -0600294int stdio_init_tables(void)
wdenk91d32562002-09-18 21:21:13 +0000295{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200296#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500297 /* already relocated for current ARM implementation */
wdenk91d32562002-09-18 21:21:13 +0000298 ulong relocation_offset = gd->reloc_off;
wdenk56958612003-06-22 17:18:28 +0000299 int i;
wdenk91d32562002-09-18 21:21:13 +0000300
301 /* relocate device name pointers */
302 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
303 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
304 relocation_offset);
305 }
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200306#endif /* CONFIG_NEEDS_MANUAL_RELOC */
wdenk91d32562002-09-18 21:21:13 +0000307
308 /* Initialize the list */
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200309 INIT_LIST_HEAD(&(devs.list));
wdenk91d32562002-09-18 21:21:13 +0000310
Simon Glass712da5e2014-09-03 17:37:01 -0600311 return 0;
312}
313
314int stdio_add_devices(void)
315{
Simon Glassd3d40352015-10-18 21:17:15 -0600316#ifdef CONFIG_DM_KEYBOARD
317 struct udevice *dev;
318 struct uclass *uc;
319 int ret;
320
321 /*
322 * For now we probe all the devices here. At some point this should be
323 * done only when the devices are required - e.g. we have a list of
324 * input devices to start up in the stdin environment variable. That
325 * work probably makes more sense when stdio itself is converted to
326 * driver model.
327 *
328 * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc.
329 * to return the device even on error. Then we could use that here.
330 */
331 ret = uclass_get(UCLASS_KEYBOARD, &uc);
332 if (ret)
333 return ret;
334
335 /* Don't report errors to the caller - assume that they are non-fatal */
336 uclass_foreach_dev(dev, uc) {
337 ret = device_probe(dev);
338 if (ret)
339 printf("Failed to probe keyboard '%s'\n", dev->name);
340 }
341#endif
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000342#ifdef CONFIG_SYS_I2C
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000343 i2c_init_all();
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000344#else
Heiko Schochere0e55bc2012-01-16 21:12:24 +0000345#endif
Simon Glass0005cb22016-01-18 19:52:23 -0700346#ifdef CONFIG_DM_VIDEO
Simon Glass4af407f2016-10-05 20:42:16 -0600347 /*
348 * If the console setting is not in environment variables then
349 * console_init_r() will not be calling iomux_doenv() (which calls
350 * search_device()). So we will not dynamically add devices by
351 * calling stdio_probe_device().
352 *
353 * So just probe all video devices now so that whichever one is
354 * required will be available.
355 */
356#ifndef CONFIG_SYS_CONSOLE_IS_IN_ENV
Simon Glass0005cb22016-01-18 19:52:23 -0700357 struct udevice *vdev;
Simon Glassc5667f42016-01-21 19:44:49 -0700358# ifndef CONFIG_DM_KEYBOARD
359 int ret;
360# endif
Simon Glass0005cb22016-01-18 19:52:23 -0700361
362 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
363 vdev;
364 ret = uclass_next_device(&vdev))
365 ;
366 if (ret)
367 printf("%s: Video device failed (ret=%d)\n", __func__, ret);
Simon Glass4af407f2016-10-05 20:42:16 -0600368#endif /* !CONFIG_SYS_CONSOLE_IS_IN_ENV */
Simon Glass0005cb22016-01-18 19:52:23 -0700369#else
370# if defined(CONFIG_LCD)
wdenk91d32562002-09-18 21:21:13 +0000371 drv_lcd_init ();
Simon Glass0005cb22016-01-18 19:52:23 -0700372# endif
373# if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
wdenk91d32562002-09-18 21:21:13 +0000374 drv_video_init ();
Simon Glass0005cb22016-01-18 19:52:23 -0700375# endif
376#endif /* CONFIG_DM_VIDEO */
Simon Glassd3d40352015-10-18 21:17:15 -0600377#if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
wdenk4e112c12003-06-03 23:54:09 +0000378 drv_keyboard_init ();
wdenk91d32562002-09-18 21:21:13 +0000379#endif
wdenk91d32562002-09-18 21:21:13 +0000380 drv_system_init ();
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200381 serial_stdio_init ();
wdenk29e7f5a2004-03-12 00:14:09 +0000382#ifdef CONFIG_USB_TTY
383 drv_usbtty_init ();
384#endif
wdenkb8fb6192004-08-02 21:11:11 +0000385#ifdef CONFIG_NETCONSOLE
386 drv_nc_init ();
387#endif
Mike Frysinger7504e972008-10-11 21:51:20 -0400388#ifdef CONFIG_JTAG_CONSOLE
389 drv_jtag_console_init ();
390#endif
Vadim Bendebury263a8bd2012-10-12 18:48:47 +0000391#ifdef CONFIG_CBMEM_CONSOLE
392 cbmemc_init();
393#endif
Simon Glass712da5e2014-09-03 17:37:01 -0600394
395 return 0;
396}
397
398int stdio_init(void)
399{
400 stdio_init_tables();
401 stdio_add_devices();
402
403 return 0;
wdenk91d32562002-09-18 21:21:13 +0000404}