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