xref: /OK3568_Linux_fs/u-boot/common/stdio.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3  *
4  * Changes for multibus/multiadapter I2C support.
5  *
6  * (C) Copyright 2000
7  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #include <config.h>
13 #include <common.h>
14 #include <dm.h>
15 #include <errno.h>
16 #include <stdarg.h>
17 #include <malloc.h>
18 #include <stdio_dev.h>
19 #include <serial.h>
20 
21 #if defined(CONFIG_SYS_I2C)
22 #include <i2c.h>
23 #endif
24 
25 #include <dm/device-internal.h>
26 
27 DECLARE_GLOBAL_DATA_PTR;
28 
29 static struct stdio_dev devs;
30 struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
31 char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
32 
33 #if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
34 #define	CONFIG_SYS_DEVICE_NULLDEV	1
35 #endif
36 
37 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
38 #define	CONFIG_SYS_DEVICE_NULLDEV	1
39 #endif
40 
41 #ifdef CONFIG_SYS_DEVICE_NULLDEV
nulldev_putc(struct stdio_dev * dev,const char c)42 static void nulldev_putc(struct stdio_dev *dev, const char c)
43 {
44 	/* nulldev is empty! */
45 }
46 
nulldev_puts(struct stdio_dev * dev,const char * s)47 static void nulldev_puts(struct stdio_dev *dev, const char *s)
48 {
49 	/* nulldev is empty! */
50 }
51 
nulldev_input(struct stdio_dev * dev)52 static int nulldev_input(struct stdio_dev *dev)
53 {
54 	/* nulldev is empty! */
55 	return 0;
56 }
57 
nulldev_clear(struct stdio_dev * dev)58 static void nulldev_clear(struct stdio_dev *dev)
59 {
60 	/* nulldev is empty! */
61 }
62 #endif
63 
stdio_serial_putc(struct stdio_dev * dev,const char c)64 static void stdio_serial_putc(struct stdio_dev *dev, const char c)
65 {
66 	serial_putc(c);
67 }
68 
stdio_serial_puts(struct stdio_dev * dev,const char * s)69 static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
70 {
71 	serial_puts(s);
72 }
73 
stdio_serial_getc(struct stdio_dev * dev)74 static int stdio_serial_getc(struct stdio_dev *dev)
75 {
76 	return serial_getc();
77 }
78 
stdio_serial_tstc(struct stdio_dev * dev)79 static int stdio_serial_tstc(struct stdio_dev *dev)
80 {
81 	return serial_tstc();
82 }
83 
stdio_serial_clear(struct stdio_dev * dev)84 static void stdio_serial_clear(struct stdio_dev *dev)
85 {
86 	serial_clear();
87 }
88 
89 /**************************************************************************
90  * SYSTEM DRIVERS
91  **************************************************************************
92  */
93 
drv_system_init(void)94 static void drv_system_init (void)
95 {
96 	struct stdio_dev dev;
97 
98 	memset (&dev, 0, sizeof (dev));
99 
100 	strcpy (dev.name, "serial");
101 	dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
102 	dev.putc = stdio_serial_putc;
103 	dev.puts = stdio_serial_puts;
104 	dev.getc = stdio_serial_getc;
105 	dev.tstc = stdio_serial_tstc;
106 	dev.clear = stdio_serial_clear;
107 	stdio_register (&dev);
108 
109 #ifdef CONFIG_SYS_DEVICE_NULLDEV
110 	memset (&dev, 0, sizeof (dev));
111 
112 	strcpy (dev.name, "nulldev");
113 	dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
114 	dev.putc = nulldev_putc;
115 	dev.puts = nulldev_puts;
116 	dev.getc = nulldev_input;
117 	dev.tstc = nulldev_input;
118 	dev.clear = nulldev_clear;
119 
120 	stdio_register (&dev);
121 #endif
122 }
123 
124 /**************************************************************************
125  * DEVICES
126  **************************************************************************
127  */
stdio_get_list(void)128 struct list_head* stdio_get_list(void)
129 {
130 	return &(devs.list);
131 }
132 
133 #ifdef CONFIG_DM_VIDEO
134 /**
135  * stdio_probe_device() - Find a device which provides the given stdio device
136  *
137  * This looks for a device of the given uclass which provides a particular
138  * stdio device. It is currently really only useful for UCLASS_VIDEO.
139  *
140  * Ultimately we want to be able to probe a device by its stdio name. At
141  * present devices register in their probe function (for video devices this
142  * is done in vidconsole_post_probe()) and we don't know what name they will
143  * use until they do so.
144  * TODO(sjg@chromium.org): We should be able to determine the name before
145  * probing, and probe the required device.
146  *
147  * @name:	stdio device name (e.g. "vidconsole")
148  * id:		Uclass ID of device to look for (e.g. UCLASS_VIDEO)
149  * @sdevp:	Returns stdout device, if found, else NULL
150  * @return 0 if found, -ENOENT if no device found with that name, other -ve
151  *	   on other error
152  */
stdio_probe_device(const char * name,enum uclass_id id,struct stdio_dev ** sdevp)153 static int stdio_probe_device(const char *name, enum uclass_id id,
154 			      struct stdio_dev **sdevp)
155 {
156 	struct stdio_dev *sdev;
157 	struct udevice *dev;
158 	int seq, ret;
159 
160 	*sdevp = NULL;
161 	seq = trailing_strtoln(name, NULL);
162 	if (seq == -1)
163 		seq = 0;
164 	ret = uclass_get_device_by_seq(id, seq, &dev);
165 	if (ret == -ENODEV)
166 		ret = uclass_first_device_err(id, &dev);
167 	if (ret) {
168 		debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
169 		      seq, name);
170 		return ret;
171 	}
172 	/* The device should be be the last one registered */
173 	sdev = list_empty(&devs.list) ? NULL :
174 			list_last_entry(&devs.list, struct stdio_dev, list);
175 	if (!sdev || strcmp(sdev->name, name)) {
176 		debug("Device '%s' did not register with stdio as '%s'\n",
177 		      dev->name, name);
178 		return -ENOENT;
179 	}
180 	*sdevp = sdev;
181 
182 	return 0;
183 }
184 #endif
185 
stdio_get_by_name(const char * name)186 struct stdio_dev *stdio_get_by_name(const char *name)
187 {
188 	struct list_head *pos;
189 	struct stdio_dev *sdev;
190 
191 	if (!name)
192 		return NULL;
193 
194 	list_for_each(pos, &(devs.list)) {
195 		sdev = list_entry(pos, struct stdio_dev, list);
196 		if (strcmp(sdev->name, name) == 0)
197 			return sdev;
198 	}
199 #ifdef CONFIG_DM_VIDEO
200 	/*
201 	 * We did not find a suitable stdio device. If there is a video
202 	 * driver with a name starting with 'vidconsole', we can try probing
203 	 * that in the hope that it will produce the required stdio device.
204 	 *
205 	 * This function is sometimes called with the entire value of
206 	 * 'stdout', which may include a list of devices separate by commas.
207 	 * Obviously this is not going to work, so we ignore that case. The
208 	 * call path in that case is console_init_r() -> search_device() ->
209 	 * stdio_get_by_name().
210 	 */
211 	if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
212 	    !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
213 		return sdev;
214 #endif
215 
216 	return NULL;
217 }
218 
stdio_clone(struct stdio_dev * dev)219 struct stdio_dev* stdio_clone(struct stdio_dev *dev)
220 {
221 	struct stdio_dev *_dev;
222 
223 	if(!dev)
224 		return NULL;
225 
226 	_dev = calloc(1, sizeof(struct stdio_dev));
227 
228 	if(!_dev)
229 		return NULL;
230 
231 	memcpy(_dev, dev, sizeof(struct stdio_dev));
232 
233 	return _dev;
234 }
235 
stdio_register_dev(struct stdio_dev * dev,struct stdio_dev ** devp)236 int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
237 {
238 	struct stdio_dev *_dev;
239 
240 	_dev = stdio_clone(dev);
241 	if(!_dev)
242 		return -ENODEV;
243 	list_add_tail(&(_dev->list), &(devs.list));
244 	if (devp)
245 		*devp = _dev;
246 
247 	return 0;
248 }
249 
stdio_register(struct stdio_dev * dev)250 int stdio_register(struct stdio_dev *dev)
251 {
252 	return stdio_register_dev(dev, NULL);
253 }
254 
255 /* deregister the device "devname".
256  * returns 0 if success, -1 if device is assigned and 1 if devname not found
257  */
258 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
stdio_deregister_dev(struct stdio_dev * dev,int force)259 int stdio_deregister_dev(struct stdio_dev *dev, int force)
260 {
261 	int l;
262 	struct list_head *pos;
263 	char temp_names[3][16];
264 
265 	/* get stdio devices (ListRemoveItem changes the dev list) */
266 	for (l=0 ; l< MAX_FILES; l++) {
267 		if (stdio_devices[l] == dev) {
268 			if (force) {
269 				strcpy(temp_names[l], "nulldev");
270 				continue;
271 			}
272 			/* Device is assigned -> report error */
273 			return -1;
274 		}
275 		memcpy (&temp_names[l][0],
276 			stdio_devices[l]->name,
277 			sizeof(temp_names[l]));
278 	}
279 
280 	list_del(&(dev->list));
281 	free(dev);
282 
283 	/* reassign Device list */
284 	list_for_each(pos, &(devs.list)) {
285 		dev = list_entry(pos, struct stdio_dev, list);
286 		for (l=0 ; l< MAX_FILES; l++) {
287 			if(strcmp(dev->name, temp_names[l]) == 0)
288 				stdio_devices[l] = dev;
289 		}
290 	}
291 	return 0;
292 }
293 
stdio_deregister(const char * devname,int force)294 int stdio_deregister(const char *devname, int force)
295 {
296 	struct stdio_dev *dev;
297 
298 	dev = stdio_get_by_name(devname);
299 
300 	if (!dev) /* device not found */
301 		return -ENODEV;
302 
303 	return stdio_deregister_dev(dev, force);
304 }
305 #endif /* CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) */
306 
stdio_init_tables(void)307 int stdio_init_tables(void)
308 {
309 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
310 	/* already relocated for current ARM implementation */
311 	ulong relocation_offset = gd->reloc_off;
312 	int i;
313 
314 	/* relocate device name pointers */
315 	for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
316 		stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
317 						relocation_offset);
318 	}
319 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
320 
321 	/* Initialize the list */
322 	INIT_LIST_HEAD(&(devs.list));
323 
324 	return 0;
325 }
326 
stdio_add_devices(void)327 int stdio_add_devices(void)
328 {
329 #ifdef CONFIG_DM_KEYBOARD
330 	struct udevice *dev;
331 	struct uclass *uc;
332 	int ret;
333 
334 	/*
335 	 * For now we probe all the devices here. At some point this should be
336 	 * done only when the devices are required - e.g. we have a list of
337 	 * input devices to start up in the stdin environment variable. That
338 	 * work probably makes more sense when stdio itself is converted to
339 	 * driver model.
340 	 *
341 	 * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc.
342 	 * to return the device even on error. Then we could use that here.
343 	 */
344 	ret = uclass_get(UCLASS_KEYBOARD, &uc);
345 	if (ret)
346 		return ret;
347 
348 	/* Don't report errors to the caller - assume that they are non-fatal */
349 	uclass_foreach_dev(dev, uc) {
350 		ret = device_probe(dev);
351 		if (ret)
352 			printf("Failed to probe keyboard '%s'\n", dev->name);
353 	}
354 #endif
355 #ifdef CONFIG_SYS_I2C
356 	i2c_init_all();
357 #else
358 #endif
359 #ifdef CONFIG_DM_VIDEO
360 	/*
361 	 * If the console setting is not in environment variables then
362 	 * console_init_r() will not be calling iomux_doenv() (which calls
363 	 * search_device()). So we will not dynamically add devices by
364 	 * calling stdio_probe_device().
365 	 *
366 	 * So just probe all video devices now so that whichever one is
367 	 * required will be available.
368 	 */
369 #ifndef CONFIG_SYS_CONSOLE_IS_IN_ENV
370 	struct udevice *vdev;
371 # ifndef CONFIG_DM_KEYBOARD
372 	int ret;
373 # endif
374 
375 	for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
376 	     vdev;
377 	     ret = uclass_next_device(&vdev))
378 		;
379 	if (ret)
380 		printf("%s: Video device failed (ret=%d)\n", __func__, ret);
381 #endif /* !CONFIG_SYS_CONSOLE_IS_IN_ENV */
382 #else
383 # if defined(CONFIG_LCD)
384 	drv_lcd_init ();
385 # endif
386 # if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
387 	drv_video_init ();
388 # endif
389 #endif /* CONFIG_DM_VIDEO */
390 #if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
391 	drv_keyboard_init ();
392 #endif
393 	drv_system_init ();
394 	serial_stdio_init ();
395 #ifdef CONFIG_USB_TTY
396 	drv_usbtty_init ();
397 #endif
398 #ifdef CONFIG_NETCONSOLE
399 	drv_nc_init ();
400 #endif
401 #ifdef CONFIG_JTAG_CONSOLE
402 	drv_jtag_console_init ();
403 #endif
404 #ifdef CONFIG_CBMEM_CONSOLE
405 	cbmemc_init();
406 #endif
407 
408 	return 0;
409 }
410 
stdio_init(void)411 int stdio_init(void)
412 {
413 	stdio_init_tables();
414 	stdio_add_devices();
415 
416 	return 0;
417 }
418