xref: /openbmc/u-boot/common/stdio.c (revision d9bef0ad)
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 <stdarg.h>
15 #include <malloc.h>
16 #include <stdio_dev.h>
17 #include <serial.h>
18 #ifdef CONFIG_LOGBUFFER
19 #include <logbuff.h>
20 #endif
21 
22 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
23 #include <i2c.h>
24 #endif
25 
26 DECLARE_GLOBAL_DATA_PTR;
27 
28 static struct stdio_dev devs;
29 struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
30 char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
31 
32 #if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
33 #define	CONFIG_SYS_DEVICE_NULLDEV	1
34 #endif
35 
36 
37 #ifdef CONFIG_SYS_DEVICE_NULLDEV
38 void nulldev_putc(const char c)
39 {
40 	/* nulldev is empty! */
41 }
42 
43 void nulldev_puts(const char *s)
44 {
45 	/* nulldev is empty! */
46 }
47 
48 int nulldev_input(void)
49 {
50 	/* nulldev is empty! */
51 	return 0;
52 }
53 #endif
54 
55 /**************************************************************************
56  * SYSTEM DRIVERS
57  **************************************************************************
58  */
59 
60 static void drv_system_init (void)
61 {
62 	struct stdio_dev dev;
63 
64 	memset (&dev, 0, sizeof (dev));
65 
66 	strcpy (dev.name, "serial");
67 	dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
68 	dev.putc = serial_putc;
69 	dev.puts = serial_puts;
70 	dev.getc = serial_getc;
71 	dev.tstc = serial_tstc;
72 	stdio_register (&dev);
73 
74 #ifdef CONFIG_SYS_DEVICE_NULLDEV
75 	memset (&dev, 0, sizeof (dev));
76 
77 	strcpy (dev.name, "nulldev");
78 	dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
79 	dev.putc = nulldev_putc;
80 	dev.puts = nulldev_puts;
81 	dev.getc = nulldev_input;
82 	dev.tstc = nulldev_input;
83 
84 	stdio_register (&dev);
85 #endif
86 }
87 
88 /**************************************************************************
89  * DEVICES
90  **************************************************************************
91  */
92 struct list_head* stdio_get_list(void)
93 {
94 	return &(devs.list);
95 }
96 
97 struct stdio_dev* stdio_get_by_name(const char *name)
98 {
99 	struct list_head *pos;
100 	struct stdio_dev *dev;
101 
102 	if(!name)
103 		return NULL;
104 
105 	list_for_each(pos, &(devs.list)) {
106 		dev = list_entry(pos, struct stdio_dev, list);
107 		if(strcmp(dev->name, name) == 0)
108 			return dev;
109 	}
110 
111 	return NULL;
112 }
113 
114 struct stdio_dev* stdio_clone(struct stdio_dev *dev)
115 {
116 	struct stdio_dev *_dev;
117 
118 	if(!dev)
119 		return NULL;
120 
121 	_dev = calloc(1, sizeof(struct stdio_dev));
122 
123 	if(!_dev)
124 		return NULL;
125 
126 	memcpy(_dev, dev, sizeof(struct stdio_dev));
127 
128 	return _dev;
129 }
130 
131 int stdio_register (struct stdio_dev * dev)
132 {
133 	struct stdio_dev *_dev;
134 
135 	_dev = stdio_clone(dev);
136 	if(!_dev)
137 		return -1;
138 	list_add_tail(&(_dev->list), &(devs.list));
139 	return 0;
140 }
141 
142 /* deregister the device "devname".
143  * returns 0 if success, -1 if device is assigned and 1 if devname not found
144  */
145 #ifdef	CONFIG_SYS_STDIO_DEREGISTER
146 int stdio_deregister(const char *devname)
147 {
148 	int l;
149 	struct list_head *pos;
150 	struct stdio_dev *dev;
151 	char temp_names[3][16];
152 
153 	dev = stdio_get_by_name(devname);
154 
155 	if(!dev) /* device not found */
156 		return -1;
157 	/* get stdio devices (ListRemoveItem changes the dev list) */
158 	for (l=0 ; l< MAX_FILES; l++) {
159 		if (stdio_devices[l] == dev) {
160 			/* Device is assigned -> report error */
161 			return -1;
162 		}
163 		memcpy (&temp_names[l][0],
164 			stdio_devices[l]->name,
165 			sizeof(temp_names[l]));
166 	}
167 
168 	list_del(&(dev->list));
169 
170 	/* reassign Device list */
171 	list_for_each(pos, &(devs.list)) {
172 		dev = list_entry(pos, struct stdio_dev, list);
173 		for (l=0 ; l< MAX_FILES; l++) {
174 			if(strcmp(dev->name, temp_names[l]) == 0)
175 				stdio_devices[l] = dev;
176 		}
177 	}
178 	return 0;
179 }
180 #endif	/* CONFIG_SYS_STDIO_DEREGISTER */
181 
182 int stdio_init (void)
183 {
184 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
185 	/* already relocated for current ARM implementation */
186 	ulong relocation_offset = gd->reloc_off;
187 	int i;
188 
189 	/* relocate device name pointers */
190 	for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
191 		stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
192 						relocation_offset);
193 	}
194 #endif /* CONFIG_NEEDS_MANUAL_RELOC */
195 
196 	/* Initialize the list */
197 	INIT_LIST_HEAD(&(devs.list));
198 
199 #ifdef CONFIG_SYS_I2C
200 	i2c_init_all();
201 #else
202 #if defined(CONFIG_HARD_I2C)
203 	i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
204 #endif
205 #endif
206 #ifdef CONFIG_LCD
207 	drv_lcd_init ();
208 #endif
209 #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
210 	drv_video_init ();
211 #endif
212 #ifdef CONFIG_KEYBOARD
213 	drv_keyboard_init ();
214 #endif
215 #ifdef CONFIG_LOGBUFFER
216 	drv_logbuff_init ();
217 #endif
218 	drv_system_init ();
219 	serial_stdio_init ();
220 #ifdef CONFIG_USB_TTY
221 	drv_usbtty_init ();
222 #endif
223 #ifdef CONFIG_NETCONSOLE
224 	drv_nc_init ();
225 #endif
226 #ifdef CONFIG_JTAG_CONSOLE
227 	drv_jtag_console_init ();
228 #endif
229 #ifdef CONFIG_CBMEM_CONSOLE
230 	cbmemc_init();
231 #endif
232 	return (0);
233 }
234