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