2 * Copyright (c) 2014 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <environment.h>
14 #include <stdio_dev.h>
17 #include <dm/device-internal.h>
21 DECLARE_GLOBAL_DATA_PTR
;
24 * Table with supported baudrates (defined in config_xyz.h)
26 static const unsigned long baudrate_table
[] = CONFIG_SYS_BAUDRATE_TABLE
;
28 #ifndef CONFIG_SYS_MALLOC_F_LEN
29 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
32 static void serial_find_console_or_panic(void)
36 #ifdef CONFIG_OF_CONTROL
39 /* Check for a chosen console */
40 node
= fdtdec_get_chosen_node(gd
->fdt_blob
, "stdout-path");
42 node
= fdt_path_offset(gd
->fdt_blob
, "console");
43 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL
, node
, &dev
)) {
44 gd
->cur_serial_dev
= dev
;
49 * If the console is not marked to be bound before relocation, bind
53 !lists_bind_fdt(gd
->dm_root
, gd
->fdt_blob
, node
, &dev
)) {
54 if (!device_probe(dev
)) {
55 gd
->cur_serial_dev
= dev
;
61 * Try to use CONFIG_CONS_INDEX if available (it is numbered from 1!).
63 * Failing that, get the device with sequence number 0, or in extremis
64 * just the first serial device we can find. But we insist on having
65 * a console (even if it is silent).
67 #ifdef CONFIG_CONS_INDEX
68 #define INDEX (CONFIG_CONS_INDEX - 1)
72 if (uclass_get_device_by_seq(UCLASS_SERIAL
, INDEX
, &dev
) &&
73 uclass_get_device(UCLASS_SERIAL
, INDEX
, &dev
) &&
74 (uclass_first_device(UCLASS_SERIAL
, &dev
) || !dev
))
75 panic("No serial driver found");
77 gd
->cur_serial_dev
= dev
;
80 /* Called prior to relocation */
83 serial_find_console_or_panic();
84 gd
->flags
|= GD_FLG_SERIAL_READY
;
89 /* Called after relocation */
90 void serial_initialize(void)
92 serial_find_console_or_panic();
95 static void _serial_putc(struct udevice
*dev
, char ch
)
97 struct dm_serial_ops
*ops
= serial_get_ops(dev
);
101 err
= ops
->putc(dev
, ch
);
102 } while (err
== -EAGAIN
);
104 _serial_putc(dev
, '\r');
107 static void _serial_puts(struct udevice
*dev
, const char *str
)
110 _serial_putc(dev
, *str
++);
113 static int _serial_getc(struct udevice
*dev
)
115 struct dm_serial_ops
*ops
= serial_get_ops(dev
);
119 err
= ops
->getc(dev
);
122 } while (err
== -EAGAIN
);
124 return err
>= 0 ? err
: 0;
127 static int _serial_tstc(struct udevice
*dev
)
129 struct dm_serial_ops
*ops
= serial_get_ops(dev
);
132 return ops
->pending(dev
, true);
137 void serial_putc(char ch
)
139 _serial_putc(gd
->cur_serial_dev
, ch
);
142 void serial_puts(const char *str
)
144 _serial_puts(gd
->cur_serial_dev
, str
);
147 int serial_getc(void)
149 return _serial_getc(gd
->cur_serial_dev
);
152 int serial_tstc(void)
154 return _serial_tstc(gd
->cur_serial_dev
);
157 void serial_setbrg(void)
159 struct dm_serial_ops
*ops
= serial_get_ops(gd
->cur_serial_dev
);
162 ops
->setbrg(gd
->cur_serial_dev
, gd
->baudrate
);
165 void serial_stdio_init(void)
169 #ifdef CONFIG_DM_STDIO
170 static void serial_stub_putc(struct stdio_dev
*sdev
, const char ch
)
172 _serial_putc(sdev
->priv
, ch
);
176 void serial_stub_puts(struct stdio_dev
*sdev
, const char *str
)
178 _serial_puts(sdev
->priv
, str
);
181 int serial_stub_getc(struct stdio_dev
*sdev
)
183 return _serial_getc(sdev
->priv
);
186 int serial_stub_tstc(struct stdio_dev
*sdev
)
188 return _serial_tstc(sdev
->priv
);
192 * on_baudrate() - Update the actual baudrate when the env var changes
194 * This will check for a valid baudrate and only apply it if valid.
196 static int on_baudrate(const char *name
, const char *value
, enum env_op op
,
204 case env_op_overwrite
:
206 * Switch to new baudrate if new baudrate is supported
208 baudrate
= simple_strtoul(value
, NULL
, 10);
210 /* Not actually changing */
211 if (gd
->baudrate
== baudrate
)
214 for (i
= 0; i
< ARRAY_SIZE(baudrate_table
); ++i
) {
215 if (baudrate
== baudrate_table
[i
])
218 if (i
== ARRAY_SIZE(baudrate_table
)) {
219 if ((flags
& H_FORCE
) == 0)
220 printf("## Baudrate %d bps not supported\n",
224 if ((flags
& H_INTERACTIVE
) != 0) {
225 printf("## Switch baudrate to %d bps and press ENTER ...\n",
230 gd
->baudrate
= baudrate
;
236 if ((flags
& H_INTERACTIVE
) != 0)
244 printf("## Baudrate may not be deleted\n");
250 U_BOOT_ENV_CALLBACK(baudrate
, on_baudrate
);
252 static int serial_post_probe(struct udevice
*dev
)
254 struct dm_serial_ops
*ops
= serial_get_ops(dev
);
255 #ifdef CONFIG_DM_STDIO
256 struct serial_dev_priv
*upriv
= dev
->uclass_priv
;
257 struct stdio_dev sdev
;
261 /* Set the baud rate */
263 ret
= ops
->setbrg(dev
, gd
->baudrate
);
268 #ifdef CONFIG_DM_STDIO
269 if (!(gd
->flags
& GD_FLG_RELOC
))
271 memset(&sdev
, '\0', sizeof(sdev
));
273 strncpy(sdev
.name
, dev
->name
, sizeof(sdev
.name
));
274 sdev
.flags
= DEV_FLAGS_OUTPUT
| DEV_FLAGS_INPUT
;
276 sdev
.putc
= serial_stub_putc
;
277 sdev
.puts
= serial_stub_puts
;
278 sdev
.getc
= serial_stub_getc
;
279 sdev
.tstc
= serial_stub_tstc
;
280 stdio_register_dev(&sdev
, &upriv
->sdev
);
285 static int serial_pre_remove(struct udevice
*dev
)
287 #ifdef CONFIG_SYS_STDIO_DEREGISTER
288 struct serial_dev_priv
*upriv
= dev
->uclass_priv
;
290 if (stdio_deregister_dev(upriv
->sdev
, 0))
297 UCLASS_DRIVER(serial
) = {
300 .post_probe
= serial_post_probe
,
301 .pre_remove
= serial_pre_remove
,
302 .per_device_auto_alloc_size
= sizeof(struct serial_dev_priv
),