Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 9 | #include <environment.h> |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <fdtdec.h> |
| 12 | #include <os.h> |
| 13 | #include <serial.h> |
| 14 | #include <stdio_dev.h> |
Simon Glass | 5c60b2a | 2014-10-22 21:37:02 -0600 | [diff] [blame] | 15 | #include <watchdog.h> |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 16 | #include <dm/lists.h> |
| 17 | #include <dm/device-internal.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 21 | /* |
| 22 | * Table with supported baudrates (defined in config_xyz.h) |
| 23 | */ |
| 24 | static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; |
| 25 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 26 | #ifndef CONFIG_SYS_MALLOC_F_LEN |
| 27 | #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work" |
| 28 | #endif |
| 29 | |
| 30 | static void serial_find_console_or_panic(void) |
| 31 | { |
Simon Glass | c860e3e | 2015-10-17 19:41:17 -0600 | [diff] [blame] | 32 | const void *blob = gd->fdt_blob; |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 33 | struct udevice *dev; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 34 | int node; |
| 35 | |
Simon Glass | afd81a5 | 2016-07-04 11:58:04 -0600 | [diff] [blame] | 36 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 37 | uclass_first_device(UCLASS_SERIAL, &dev); |
| 38 | if (dev) { |
| 39 | gd->cur_serial_dev = dev; |
| 40 | return; |
| 41 | } |
| 42 | } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) { |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 43 | /* Check for a chosen console */ |
Simon Glass | c860e3e | 2015-10-17 19:41:17 -0600 | [diff] [blame] | 44 | node = fdtdec_get_chosen_node(blob, "stdout-path"); |
| 45 | if (node < 0) { |
| 46 | const char *str, *p, *name; |
| 47 | |
| 48 | /* |
| 49 | * Deal with things like |
| 50 | * stdout-path = "serial0:115200n8"; |
| 51 | * |
| 52 | * We need to look up the alias and then follow it to |
| 53 | * the correct node. |
| 54 | */ |
| 55 | str = fdtdec_get_chosen_prop(blob, "stdout-path"); |
| 56 | if (str) { |
| 57 | p = strchr(str, ':'); |
| 58 | name = fdt_get_alias_namelen(blob, str, |
| 59 | p ? p - str : strlen(str)); |
| 60 | if (name) |
| 61 | node = fdt_path_offset(blob, name); |
| 62 | } |
| 63 | } |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 64 | if (node < 0) |
Simon Glass | c860e3e | 2015-10-17 19:41:17 -0600 | [diff] [blame] | 65 | node = fdt_path_offset(blob, "console"); |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 66 | if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, |
| 67 | &dev)) { |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 68 | gd->cur_serial_dev = dev; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 69 | return; |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 70 | } |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 71 | |
| 72 | /* |
Stefan Roese | 88c3a88 | 2015-11-26 13:38:43 +0100 | [diff] [blame] | 73 | * If the console is not marked to be bound before relocation, |
| 74 | * bind it anyway. |
| 75 | */ |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 76 | if (node > 0 && |
Simon Glass | 802b2c6 | 2017-05-18 20:09:06 -0600 | [diff] [blame] | 77 | !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node), |
| 78 | &dev)) { |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 79 | if (!device_probe(dev)) { |
| 80 | gd->cur_serial_dev = dev; |
| 81 | return; |
| 82 | } |
| 83 | } |
Simon Glass | 8579a51 | 2015-03-06 13:19:03 -0700 | [diff] [blame] | 84 | } |
Simon Glass | c860e3e | 2015-10-17 19:41:17 -0600 | [diff] [blame] | 85 | if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) { |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 86 | /* |
Stefan Roese | 88c3a88 | 2015-11-26 13:38:43 +0100 | [diff] [blame] | 87 | * Try to use CONFIG_CONS_INDEX if available (it is numbered |
| 88 | * from 1!). |
| 89 | * |
| 90 | * Failing that, get the device with sequence number 0, or in |
| 91 | * extremis just the first serial device we can find. But we |
| 92 | * insist on having a console (even if it is silent). |
| 93 | */ |
Simon Glass | bc5aa99 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 94 | #ifdef CONFIG_CONS_INDEX |
| 95 | #define INDEX (CONFIG_CONS_INDEX - 1) |
| 96 | #else |
| 97 | #define INDEX 0 |
| 98 | #endif |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 99 | if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || |
| 100 | !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) || |
Simon Glass | 6664077 | 2015-07-31 09:31:20 -0600 | [diff] [blame] | 101 | (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) { |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 102 | gd->cur_serial_dev = dev; |
| 103 | return; |
| 104 | } |
Simon Glass | bc5aa99 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 105 | #undef INDEX |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 106 | } |
| 107 | |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 108 | #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE |
Simon Glass | c62fb99 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 109 | panic_str("No serial driver found"); |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 110 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Called prior to relocation */ |
| 114 | int serial_init(void) |
| 115 | { |
| 116 | serial_find_console_or_panic(); |
| 117 | gd->flags |= GD_FLG_SERIAL_READY; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* Called after relocation */ |
| 123 | void serial_initialize(void) |
| 124 | { |
Simon Glass | 51f73f1 | 2016-03-11 22:06:51 -0700 | [diff] [blame] | 125 | serial_init(); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 126 | } |
| 127 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 128 | static void _serial_putc(struct udevice *dev, char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 129 | { |
Masahiro Yamada | 84df41a | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 130 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 131 | int err; |
| 132 | |
Alison Wang | c0a9ce6 | 2016-03-02 11:00:36 +0800 | [diff] [blame] | 133 | if (ch == '\n') |
| 134 | _serial_putc(dev, '\r'); |
| 135 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 136 | do { |
Masahiro Yamada | 84df41a | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 137 | err = ops->putc(dev, ch); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 138 | } while (err == -EAGAIN); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 139 | } |
| 140 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 141 | static void _serial_puts(struct udevice *dev, const char *str) |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 142 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 143 | while (*str) |
| 144 | _serial_putc(dev, *str++); |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 145 | } |
| 146 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 147 | static int _serial_getc(struct udevice *dev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 148 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 149 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 150 | int err; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 151 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 152 | do { |
| 153 | err = ops->getc(dev); |
| 154 | if (err == -EAGAIN) |
| 155 | WATCHDOG_RESET(); |
| 156 | } while (err == -EAGAIN); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 157 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 158 | return err >= 0 ? err : 0; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 159 | } |
| 160 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 161 | static int _serial_tstc(struct udevice *dev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 162 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 163 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 164 | |
| 165 | if (ops->pending) |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 166 | return ops->pending(dev, true); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 167 | |
| 168 | return 1; |
| 169 | } |
| 170 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 171 | void serial_putc(char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 172 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 173 | if (gd->cur_serial_dev) |
| 174 | _serial_putc(gd->cur_serial_dev, ch); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 175 | } |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 176 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 177 | void serial_puts(const char *str) |
| 178 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 179 | if (gd->cur_serial_dev) |
| 180 | _serial_puts(gd->cur_serial_dev, str); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 181 | } |
| 182 | |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 183 | int serial_getc(void) |
| 184 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 185 | if (!gd->cur_serial_dev) |
| 186 | return 0; |
| 187 | |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 188 | return _serial_getc(gd->cur_serial_dev); |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 189 | } |
| 190 | |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 191 | int serial_tstc(void) |
| 192 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 193 | if (!gd->cur_serial_dev) |
| 194 | return 0; |
| 195 | |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 196 | return _serial_tstc(gd->cur_serial_dev); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void serial_setbrg(void) |
| 200 | { |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 201 | struct dm_serial_ops *ops; |
| 202 | |
| 203 | if (!gd->cur_serial_dev) |
| 204 | return; |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 205 | |
Hans de Goede | 04f8094 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 206 | ops = serial_get_ops(gd->cur_serial_dev); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 207 | if (ops->setbrg) |
Simon Glass | a4c2e13 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 208 | ops->setbrg(gd->cur_serial_dev, gd->baudrate); |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 209 | } |
| 210 | |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 211 | void serial_stdio_init(void) |
| 212 | { |
| 213 | } |
| 214 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame^] | 215 | #if defined(CONFIG_DM_STDIO) |
| 216 | |
| 217 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 73285a2 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 218 | static void serial_stub_putc(struct stdio_dev *sdev, const char ch) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 219 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 220 | _serial_putc(sdev->priv, ch); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 221 | } |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 222 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 223 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame^] | 224 | static void serial_stub_puts(struct stdio_dev *sdev, const char *str) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 225 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 226 | _serial_puts(sdev->priv, str); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 227 | } |
| 228 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame^] | 229 | static int serial_stub_getc(struct stdio_dev *sdev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 230 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 231 | return _serial_getc(sdev->priv); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 232 | } |
| 233 | |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame^] | 234 | static int serial_stub_tstc(struct stdio_dev *sdev) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 235 | { |
Masahiro Yamada | 93da2fc | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 236 | return _serial_tstc(sdev->priv); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 237 | } |
Masahiro Yamada | 5e0ef84 | 2017-06-22 16:48:49 +0900 | [diff] [blame^] | 238 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 239 | |
Simon Glass | 593f50b | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 240 | /** |
| 241 | * on_baudrate() - Update the actual baudrate when the env var changes |
| 242 | * |
| 243 | * This will check for a valid baudrate and only apply it if valid. |
| 244 | */ |
| 245 | static int on_baudrate(const char *name, const char *value, enum env_op op, |
| 246 | int flags) |
| 247 | { |
| 248 | int i; |
| 249 | int baudrate; |
| 250 | |
| 251 | switch (op) { |
| 252 | case env_op_create: |
| 253 | case env_op_overwrite: |
| 254 | /* |
| 255 | * Switch to new baudrate if new baudrate is supported |
| 256 | */ |
| 257 | baudrate = simple_strtoul(value, NULL, 10); |
| 258 | |
| 259 | /* Not actually changing */ |
| 260 | if (gd->baudrate == baudrate) |
| 261 | return 0; |
| 262 | |
| 263 | for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { |
| 264 | if (baudrate == baudrate_table[i]) |
| 265 | break; |
| 266 | } |
| 267 | if (i == ARRAY_SIZE(baudrate_table)) { |
| 268 | if ((flags & H_FORCE) == 0) |
| 269 | printf("## Baudrate %d bps not supported\n", |
| 270 | baudrate); |
| 271 | return 1; |
| 272 | } |
| 273 | if ((flags & H_INTERACTIVE) != 0) { |
| 274 | printf("## Switch baudrate to %d bps and press ENTER ...\n", |
| 275 | baudrate); |
| 276 | udelay(50000); |
| 277 | } |
| 278 | |
| 279 | gd->baudrate = baudrate; |
| 280 | |
| 281 | serial_setbrg(); |
| 282 | |
| 283 | udelay(50000); |
| 284 | |
| 285 | if ((flags & H_INTERACTIVE) != 0) |
| 286 | while (1) { |
| 287 | if (getc() == '\r') |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | case env_op_delete: |
| 293 | printf("## Baudrate may not be deleted\n"); |
| 294 | return 1; |
| 295 | default: |
| 296 | return 0; |
| 297 | } |
| 298 | } |
| 299 | U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 300 | |
Simon Glass | 30a1349 | 2015-12-13 21:36:58 -0700 | [diff] [blame] | 301 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 302 | static int serial_post_probe(struct udevice *dev) |
| 303 | { |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 304 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 305 | #ifdef CONFIG_DM_STDIO |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 306 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 307 | struct stdio_dev sdev; |
| 308 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 309 | int ret; |
| 310 | |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 311 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 312 | if (ops->setbrg) |
| 313 | ops->setbrg += gd->reloc_off; |
| 314 | if (ops->getc) |
| 315 | ops->getc += gd->reloc_off; |
| 316 | if (ops->putc) |
| 317 | ops->putc += gd->reloc_off; |
| 318 | if (ops->pending) |
| 319 | ops->pending += gd->reloc_off; |
| 320 | if (ops->clear) |
| 321 | ops->clear += gd->reloc_off; |
| 322 | #if CONFIG_POST & CONFIG_SYS_POST_UART |
| 323 | if (ops->loop) |
| 324 | ops->loop += gd->reloc_off |
| 325 | #endif |
| 326 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 327 | /* Set the baud rate */ |
| 328 | if (ops->setbrg) { |
| 329 | ret = ops->setbrg(dev, gd->baudrate); |
| 330 | if (ret) |
| 331 | return ret; |
| 332 | } |
| 333 | |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 334 | #ifdef CONFIG_DM_STDIO |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 335 | if (!(gd->flags & GD_FLG_RELOC)) |
| 336 | return 0; |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 337 | memset(&sdev, '\0', sizeof(sdev)); |
| 338 | |
| 339 | strncpy(sdev.name, dev->name, sizeof(sdev.name)); |
| 340 | sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
| 341 | sdev.priv = dev; |
| 342 | sdev.putc = serial_stub_putc; |
| 343 | sdev.puts = serial_stub_puts; |
| 344 | sdev.getc = serial_stub_getc; |
| 345 | sdev.tstc = serial_stub_tstc; |
| 346 | stdio_register_dev(&sdev, &upriv->sdev); |
Simon Glass | c058d8b | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 347 | #endif |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int serial_pre_remove(struct udevice *dev) |
| 352 | { |
Simon Glass | 3110ece | 2016-10-17 20:13:02 -0600 | [diff] [blame] | 353 | #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 354 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 355 | |
Stefan Roese | dcc4b07 | 2017-04-24 09:48:01 +0200 | [diff] [blame] | 356 | if (stdio_deregister_dev(upriv->sdev, true)) |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 357 | return -EPERM; |
| 358 | #endif |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | UCLASS_DRIVER(serial) = { |
| 364 | .id = UCLASS_SERIAL, |
| 365 | .name = "serial", |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 366 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 247f596 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 367 | .post_probe = serial_post_probe, |
| 368 | .pre_remove = serial_pre_remove, |
| 369 | .per_device_auto_alloc_size = sizeof(struct serial_dev_priv), |
| 370 | }; |
Simon Glass | 30a1349 | 2015-12-13 21:36:58 -0700 | [diff] [blame] | 371 | #endif |