]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
91d3256c | 2 | /* |
3f4978c7 HS |
3 | * Copyright (C) 2009 Sergey Kubushyn <[email protected]> |
4 | * | |
5 | * Changes for multibus/multiadapter I2C support. | |
6 | * | |
91d3256c WD |
7 | * (C) Copyright 2000 |
8 | * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), [email protected] | |
91d3256c WD |
9 | */ |
10 | ||
11 | #include <config.h> | |
b206cd73 | 12 | #include <dm.h> |
d97143a6 | 13 | #include <errno.h> |
f7ae49fc | 14 | #include <log.h> |
91d3256c WD |
15 | #include <stdarg.h> |
16 | #include <malloc.h> | |
52cb4d4f | 17 | #include <stdio_dev.h> |
281e00a3 | 18 | #include <serial.h> |
5eb83c0a | 19 | #include <splash.h> |
91d3256c | 20 | #include <i2c.h> |
401d1c4f | 21 | #include <asm/global_data.h> |
b206cd73 SG |
22 | #include <dm/device-internal.h> |
23 | ||
d87080b7 WD |
24 | DECLARE_GLOBAL_DATA_PTR; |
25 | ||
52cb4d4f JCPV |
26 | static struct stdio_dev devs; |
27 | struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL }; | |
91d3256c WD |
28 | char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" }; |
29 | ||
d9b0ac90 AS |
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 | ||
99cb2b99 | 43 | #if CONFIG_IS_ENABLED(SYS_DEVICE_NULLDEV) |
654f8d0f | 44 | static void nulldev_putc(struct stdio_dev *dev, const char c) |
91d3256c | 45 | { |
c1de7a6d | 46 | /* nulldev is empty! */ |
91d3256c WD |
47 | } |
48 | ||
654f8d0f | 49 | static void nulldev_puts(struct stdio_dev *dev, const char *s) |
91d3256c | 50 | { |
c1de7a6d | 51 | /* nulldev is empty! */ |
91d3256c WD |
52 | } |
53 | ||
654f8d0f | 54 | static int nulldev_input(struct stdio_dev *dev) |
91d3256c | 55 | { |
c1de7a6d JCPV |
56 | /* nulldev is empty! */ |
57 | return 0; | |
91d3256c | 58 | } |
91d3256c | 59 | |
99cb2b99 AS |
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 | |
76 | static inline void nulldev_register(void) {} | |
77 | #endif /* SYS_DEVICE_NULLDEV */ | |
78 | ||
654f8d0f | 79 | static void stdio_serial_putc(struct stdio_dev *dev, const char c) |
709ea543 SG |
80 | { |
81 | serial_putc(c); | |
82 | } | |
83 | ||
654f8d0f | 84 | static void stdio_serial_puts(struct stdio_dev *dev, const char *s) |
709ea543 SG |
85 | { |
86 | serial_puts(s); | |
87 | } | |
88 | ||
78b52431 T |
89 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
90 | static void stdio_serial_flush(struct stdio_dev *dev) | |
91 | { | |
92 | serial_flush(); | |
93 | } | |
94 | #endif | |
95 | ||
654f8d0f | 96 | static int stdio_serial_getc(struct stdio_dev *dev) |
709ea543 SG |
97 | { |
98 | return serial_getc(); | |
99 | } | |
100 | ||
654f8d0f | 101 | static int stdio_serial_tstc(struct stdio_dev *dev) |
709ea543 SG |
102 | { |
103 | return serial_tstc(); | |
104 | } | |
105 | ||
91d3256c WD |
106 | /************************************************************************** |
107 | * SYSTEM DRIVERS | |
108 | ************************************************************************** | |
109 | */ | |
110 | ||
111 | static void drv_system_init (void) | |
112 | { | |
52cb4d4f | 113 | struct stdio_dev dev; |
91d3256c WD |
114 | |
115 | memset (&dev, 0, sizeof (dev)); | |
116 | ||
117 | strcpy (dev.name, "serial"); | |
1caf934a | 118 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
709ea543 SG |
119 | dev.putc = stdio_serial_putc; |
120 | dev.puts = stdio_serial_puts; | |
78b52431 | 121 | STDIO_DEV_ASSIGN_FLUSH(&dev, stdio_serial_flush); |
709ea543 SG |
122 | dev.getc = stdio_serial_getc; |
123 | dev.tstc = stdio_serial_tstc; | |
52cb4d4f | 124 | stdio_register (&dev); |
91d3256c | 125 | |
99cb2b99 | 126 | nulldev_register(); |
91d3256c WD |
127 | } |
128 | ||
129 | /************************************************************************** | |
130 | * DEVICES | |
131 | ************************************************************************** | |
132 | */ | |
52cb4d4f | 133 | struct list_head* stdio_get_list(void) |
c1de7a6d | 134 | { |
18c587d0 | 135 | return &devs.list; |
c1de7a6d JCPV |
136 | } |
137 | ||
d8441ea2 SG |
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([email protected]): 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 | |
185f812c | 154 | * Return: 0 if found, -ENOENT if no device found with that name, other -ve |
d8441ea2 SG |
155 | * on other error |
156 | */ | |
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) | |
d844efec SG |
167 | seq = 0; |
168 | ret = uclass_get_device_by_seq(id, seq, &dev); | |
169 | if (ret == -ENODEV) | |
d8441ea2 | 170 | ret = uclass_first_device_err(id, &dev); |
d8441ea2 SG |
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 | } | |
d8441ea2 | 188 | |
ab29a34a | 189 | struct stdio_dev *stdio_get_by_name(const char *name) |
c1de7a6d JCPV |
190 | { |
191 | struct list_head *pos; | |
d8441ea2 | 192 | struct stdio_dev *sdev; |
c1de7a6d | 193 | |
ab29a34a | 194 | if (!name) |
c1de7a6d JCPV |
195 | return NULL; |
196 | ||
18c587d0 | 197 | list_for_each(pos, &devs.list) { |
d8441ea2 SG |
198 | sdev = list_entry(pos, struct stdio_dev, list); |
199 | if (strcmp(sdev->name, name) == 0) | |
200 | return sdev; | |
c1de7a6d | 201 | } |
b86986c7 | 202 | if (IS_ENABLED(CONFIG_VIDEO)) { |
5d4b6b17 SG |
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 | |
3232487d | 213 | * console_init_r() -> console_search_dev() -> stdio_get_by_name() |
5d4b6b17 SG |
214 | */ |
215 | if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') && | |
216 | !stdio_probe_device(name, UCLASS_VIDEO, &sdev)) | |
217 | return sdev; | |
218 | } | |
c1de7a6d JCPV |
219 | |
220 | return NULL; | |
221 | } | |
222 | ||
4225f2f5 | 223 | struct stdio_dev *stdio_clone(struct stdio_dev *dev) |
628ffd73 | 224 | { |
52cb4d4f | 225 | struct stdio_dev *_dev; |
628ffd73 | 226 | |
4225f2f5 | 227 | if (!dev) |
628ffd73 JCPV |
228 | return NULL; |
229 | ||
52cb4d4f | 230 | _dev = calloc(1, sizeof(struct stdio_dev)); |
4225f2f5 | 231 | if (!_dev) |
628ffd73 JCPV |
232 | return NULL; |
233 | ||
52cb4d4f | 234 | memcpy(_dev, dev, sizeof(struct stdio_dev)); |
628ffd73 JCPV |
235 | |
236 | return _dev; | |
237 | } | |
91d3256c | 238 | |
d97143a6 | 239 | int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp) |
91d3256c | 240 | { |
52cb4d4f | 241 | struct stdio_dev *_dev; |
628ffd73 | 242 | |
52cb4d4f | 243 | _dev = stdio_clone(dev); |
4225f2f5 | 244 | if (!_dev) |
d97143a6 | 245 | return -ENODEV; |
18c587d0 | 246 | list_add_tail(&_dev->list, &devs.list); |
d97143a6 SG |
247 | if (devp) |
248 | *devp = _dev; | |
249 | ||
91d3256c WD |
250 | return 0; |
251 | } | |
252 | ||
d97143a6 SG |
253 | int stdio_register(struct stdio_dev *dev) |
254 | { | |
255 | return stdio_register_dev(dev, NULL); | |
256 | } | |
257 | ||
32d01926 | 258 | int stdio_deregister_dev(struct stdio_dev *dev, int force) |
91d3256c | 259 | { |
c1de7a6d | 260 | struct list_head *pos; |
6da11cc8 | 261 | char temp_names[3][STDIO_NAME_LEN]; |
4225f2f5 | 262 | int i; |
91d3256c | 263 | |
91d3256c | 264 | /* get stdio devices (ListRemoveItem changes the dev list) */ |
4225f2f5 SG |
265 | for (i = 0 ; i < MAX_FILES; i++) { |
266 | if (stdio_devices[i] == dev) { | |
32d01926 | 267 | if (force) { |
4225f2f5 | 268 | strcpy(temp_names[i], "nulldev"); |
32d01926 HG |
269 | continue; |
270 | } | |
91d3256c | 271 | /* Device is assigned -> report error */ |
4225f2f5 | 272 | return -EBUSY; |
91d3256c | 273 | } |
6da11cc8 HS |
274 | strlcpy(&temp_names[i][0], stdio_devices[i]->name, |
275 | sizeof(temp_names[i])); | |
91d3256c | 276 | } |
c1de7a6d | 277 | |
18c587d0 | 278 | list_del(&dev->list); |
88274b6c | 279 | free(dev); |
c1de7a6d | 280 | |
4225f2f5 | 281 | /* reassign device list */ |
18c587d0 | 282 | list_for_each(pos, &devs.list) { |
52cb4d4f | 283 | dev = list_entry(pos, struct stdio_dev, list); |
4225f2f5 SG |
284 | for (i = 0 ; i < MAX_FILES; i++) { |
285 | if (strcmp(dev->name, temp_names[i]) == 0) | |
286 | stdio_devices[i] = dev; | |
91d3256c WD |
287 | } |
288 | } | |
4225f2f5 | 289 | |
91d3256c WD |
290 | return 0; |
291 | } | |
d97143a6 | 292 | |
9fb02491 | 293 | int stdio_init_tables(void) |
91d3256c | 294 | { |
91d3256c | 295 | /* Initialize the list */ |
18c587d0 | 296 | INIT_LIST_HEAD(&devs.list); |
91d3256c | 297 | |
9fb02491 SG |
298 | return 0; |
299 | } | |
300 | ||
301 | int stdio_add_devices(void) | |
302 | { | |
b206cd73 | 303 | struct udevice *dev; |
b206cd73 SG |
304 | int ret; |
305 | ||
5d4b6b17 SG |
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. | |
5d4b6b17 | 313 | */ |
5d4b6b17 SG |
314 | |
315 | /* | |
316 | * Don't report errors to the caller - assume that they are | |
317 | * non-fatal | |
318 | */ | |
7ff12631 MS |
319 | for (ret = uclass_first_device_check(UCLASS_KEYBOARD, &dev); |
320 | dev; | |
321 | ret = uclass_next_device_check(&dev)) { | |
5d4b6b17 | 322 | if (ret) |
7ff12631 MS |
323 | printf("%s: Failed to probe keyboard '%s' (ret=%d)\n", |
324 | __func__, dev->name, ret); | |
5d4b6b17 | 325 | } |
b206cd73 | 326 | } |
55dabcc8 | 327 | #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY) |
3f4978c7 | 328 | i2c_init_all(); |
3f4978c7 | 329 | #endif |
b86986c7 | 330 | if (IS_ENABLED(CONFIG_VIDEO)) { |
5d4b6b17 SG |
331 | /* |
332 | * If the console setting is not in environment variables then | |
333 | * console_init_r() will not be calling iomux_doenv() (which | |
3232487d | 334 | * calls console_search_dev()). So we will not dynamically add |
5d4b6b17 SG |
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)) { | |
7ff12631 MS |
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 | } | |
5d4b6b17 SG |
352 | } |
353 | if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && | |
354 | IS_ENABLED(CONFIG_CMD_BMP)) | |
355 | splash_display(); | |
5d4b6b17 SG |
356 | } |
357 | ||
5d4b6b17 SG |
358 | drv_system_init(); |
359 | serial_stdio_init(); | |
232c150a | 360 | #ifdef CONFIG_USB_TTY |
5d4b6b17 | 361 | drv_usbtty_init(); |
fc2b399a LP |
362 | #endif |
363 | #ifdef CONFIG_USB_FUNCTION_ACM | |
364 | drv_usbacm_init (); | |
68ceb29e | 365 | #endif |
5d4b6b17 SG |
366 | if (IS_ENABLED(CONFIG_NETCONSOLE)) |
367 | drv_nc_init(); | |
36ea8e9a | 368 | #ifdef CONFIG_JTAG_CONSOLE |
5d4b6b17 | 369 | drv_jtag_console_init(); |
98ab435f | 370 | #endif |
5d4b6b17 SG |
371 | if (IS_ENABLED(CONFIG_CBMEM_CONSOLE)) |
372 | cbmemc_init(); | |
9fb02491 SG |
373 | |
374 | return 0; | |
375 | } |