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