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