Simon Glass | 57d9275 | 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 | ad1b81c | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 9 | #include <environment.h> |
Simon Glass | 57d9275 | 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 | c487fd4 | 2014-10-22 21:37:02 -0600 | [diff] [blame] | 15 | #include <watchdog.h> |
Simon Glass | 57d9275 | 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 | ad1b81c | 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 | 57d9275 | 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 | 469a579 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 32 | struct udevice *dev; |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 33 | int node; |
| 34 | |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 35 | if (CONFIG_IS_ENABLED(OF_CONTROL) && gd->fdt_blob) { |
Simon Glass | e9fc058 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 36 | /* Check for a chosen console */ |
| 37 | node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path"); |
| 38 | if (node < 0) |
| 39 | node = fdt_path_offset(gd->fdt_blob, "console"); |
| 40 | if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, |
| 41 | &dev)) { |
Simon Glass | 469a579 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 42 | gd->cur_serial_dev = dev; |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 43 | return; |
Simon Glass | 469a579 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 44 | } |
Simon Glass | e9fc058 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * If the console is not marked to be bound before relocation, |
| 48 | * bind it anyway. |
| 49 | */ |
| 50 | if (node > 0 && |
| 51 | !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &dev)) { |
| 52 | if (!device_probe(dev)) { |
| 53 | gd->cur_serial_dev = dev; |
| 54 | return; |
| 55 | } |
| 56 | } |
Simon Glass | af28224 | 2015-03-06 13:19:03 -0700 | [diff] [blame] | 57 | } |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 58 | if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !gd->fdt_blob) { |
Simon Glass | e9fc058 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 59 | /* |
| 60 | * Try to use CONFIG_CONS_INDEX if available (it is numbered |
| 61 | * from 1!). |
| 62 | * |
| 63 | * Failing that, get the device with sequence number 0, or in |
| 64 | * extremis just the first serial device we can find. But we |
| 65 | * insist on having a console (even if it is silent). |
| 66 | */ |
Simon Glass | 91155c6 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 67 | #ifdef CONFIG_CONS_INDEX |
| 68 | #define INDEX (CONFIG_CONS_INDEX - 1) |
| 69 | #else |
| 70 | #define INDEX 0 |
| 71 | #endif |
Simon Glass | e9fc058 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 72 | if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || |
| 73 | !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) || |
Simon Glass | 753812c | 2015-07-31 09:31:20 -0600 | [diff] [blame] | 74 | (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) { |
Simon Glass | e9fc058 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 75 | gd->cur_serial_dev = dev; |
| 76 | return; |
| 77 | } |
Simon Glass | 91155c6 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 78 | #undef INDEX |
Simon Glass | e9fc058 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Hans de Goede | 8c45858 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 81 | #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE |
Simon Glass | e9fc058 | 2015-05-12 14:55:05 -0600 | [diff] [blame] | 82 | panic_str("No serial driver found"); |
Hans de Goede | 8c45858 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 83 | #endif |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Called prior to relocation */ |
| 87 | int serial_init(void) |
| 88 | { |
| 89 | serial_find_console_or_panic(); |
| 90 | gd->flags |= GD_FLG_SERIAL_READY; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* Called after relocation */ |
| 96 | void serial_initialize(void) |
| 97 | { |
| 98 | serial_find_console_or_panic(); |
| 99 | } |
| 100 | |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 101 | static void _serial_putc(struct udevice *dev, char ch) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 102 | { |
Masahiro Yamada | bac6446 | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 103 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 104 | int err; |
| 105 | |
| 106 | do { |
Masahiro Yamada | bac6446 | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 107 | err = ops->putc(dev, ch); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 108 | } while (err == -EAGAIN); |
| 109 | if (ch == '\n') |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 110 | _serial_putc(dev, '\r'); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 111 | } |
| 112 | |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 113 | static void _serial_puts(struct udevice *dev, const char *str) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 114 | { |
| 115 | while (*str) |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 116 | _serial_putc(dev, *str++); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 117 | } |
| 118 | |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 119 | static int _serial_getc(struct udevice *dev) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 120 | { |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 121 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 122 | int err; |
| 123 | |
| 124 | do { |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 125 | err = ops->getc(dev); |
Simon Glass | c487fd4 | 2014-10-22 21:37:02 -0600 | [diff] [blame] | 126 | if (err == -EAGAIN) |
| 127 | WATCHDOG_RESET(); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 128 | } while (err == -EAGAIN); |
| 129 | |
| 130 | return err >= 0 ? err : 0; |
| 131 | } |
| 132 | |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 133 | static int _serial_tstc(struct udevice *dev) |
| 134 | { |
| 135 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 136 | |
| 137 | if (ops->pending) |
| 138 | return ops->pending(dev, true); |
| 139 | |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | void serial_putc(char ch) |
| 144 | { |
Hans de Goede | 8c45858 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 145 | if (gd->cur_serial_dev) |
| 146 | _serial_putc(gd->cur_serial_dev, ch); |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void serial_puts(const char *str) |
| 150 | { |
Hans de Goede | 8c45858 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 151 | if (gd->cur_serial_dev) |
| 152 | _serial_puts(gd->cur_serial_dev, str); |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 155 | int serial_getc(void) |
| 156 | { |
Hans de Goede | 8c45858 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 157 | if (!gd->cur_serial_dev) |
| 158 | return 0; |
| 159 | |
Simon Glass | 469a579 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 160 | return _serial_getc(gd->cur_serial_dev); |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int serial_tstc(void) |
| 164 | { |
Hans de Goede | 8c45858 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 165 | if (!gd->cur_serial_dev) |
| 166 | return 0; |
| 167 | |
Simon Glass | 469a579 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 168 | return _serial_tstc(gd->cur_serial_dev); |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void serial_setbrg(void) |
| 172 | { |
Hans de Goede | 8c45858 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 173 | struct dm_serial_ops *ops; |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 174 | |
Hans de Goede | 8c45858 | 2015-08-08 17:45:18 +0200 | [diff] [blame] | 175 | if (!gd->cur_serial_dev) |
| 176 | return; |
| 177 | |
| 178 | ops = serial_get_ops(gd->cur_serial_dev); |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 179 | if (ops->setbrg) |
Simon Glass | 469a579 | 2014-11-10 18:00:20 -0700 | [diff] [blame] | 180 | ops->setbrg(gd->cur_serial_dev, gd->baudrate); |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 181 | } |
| 182 | |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 183 | void serial_stdio_init(void) |
| 184 | { |
| 185 | } |
| 186 | |
Simon Glass | 236f2bd | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 187 | #ifdef CONFIG_DM_STDIO |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 188 | static void serial_stub_putc(struct stdio_dev *sdev, const char ch) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 189 | { |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 190 | _serial_putc(sdev->priv, ch); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 191 | } |
Simon Glass | 236f2bd | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 192 | #endif |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 193 | |
| 194 | void serial_stub_puts(struct stdio_dev *sdev, const char *str) |
| 195 | { |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 196 | _serial_puts(sdev->priv, str); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | int serial_stub_getc(struct stdio_dev *sdev) |
| 200 | { |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 201 | return _serial_getc(sdev->priv); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int serial_stub_tstc(struct stdio_dev *sdev) |
| 205 | { |
Masahiro Yamada | eea5a4c | 2014-10-23 22:26:08 +0900 | [diff] [blame] | 206 | return _serial_tstc(sdev->priv); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 207 | } |
| 208 | |
Simon Glass | ad1b81c | 2014-10-29 13:09:03 -0600 | [diff] [blame] | 209 | /** |
| 210 | * on_baudrate() - Update the actual baudrate when the env var changes |
| 211 | * |
| 212 | * This will check for a valid baudrate and only apply it if valid. |
| 213 | */ |
| 214 | static int on_baudrate(const char *name, const char *value, enum env_op op, |
| 215 | int flags) |
| 216 | { |
| 217 | int i; |
| 218 | int baudrate; |
| 219 | |
| 220 | switch (op) { |
| 221 | case env_op_create: |
| 222 | case env_op_overwrite: |
| 223 | /* |
| 224 | * Switch to new baudrate if new baudrate is supported |
| 225 | */ |
| 226 | baudrate = simple_strtoul(value, NULL, 10); |
| 227 | |
| 228 | /* Not actually changing */ |
| 229 | if (gd->baudrate == baudrate) |
| 230 | return 0; |
| 231 | |
| 232 | for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { |
| 233 | if (baudrate == baudrate_table[i]) |
| 234 | break; |
| 235 | } |
| 236 | if (i == ARRAY_SIZE(baudrate_table)) { |
| 237 | if ((flags & H_FORCE) == 0) |
| 238 | printf("## Baudrate %d bps not supported\n", |
| 239 | baudrate); |
| 240 | return 1; |
| 241 | } |
| 242 | if ((flags & H_INTERACTIVE) != 0) { |
| 243 | printf("## Switch baudrate to %d bps and press ENTER ...\n", |
| 244 | baudrate); |
| 245 | udelay(50000); |
| 246 | } |
| 247 | |
| 248 | gd->baudrate = baudrate; |
| 249 | |
| 250 | serial_setbrg(); |
| 251 | |
| 252 | udelay(50000); |
| 253 | |
| 254 | if ((flags & H_INTERACTIVE) != 0) |
| 255 | while (1) { |
| 256 | if (getc() == '\r') |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | case env_op_delete: |
| 262 | printf("## Baudrate may not be deleted\n"); |
| 263 | return 1; |
| 264 | default: |
| 265 | return 0; |
| 266 | } |
| 267 | } |
| 268 | U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); |
| 269 | |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 270 | static int serial_post_probe(struct udevice *dev) |
| 271 | { |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 272 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 236f2bd | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 273 | #ifdef CONFIG_DM_STDIO |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 274 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | 236f2bd | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 275 | struct stdio_dev sdev; |
| 276 | #endif |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 277 | int ret; |
| 278 | |
Michal Simek | 484fdf5 | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 279 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 280 | if (ops->setbrg) |
| 281 | ops->setbrg += gd->reloc_off; |
| 282 | if (ops->getc) |
| 283 | ops->getc += gd->reloc_off; |
| 284 | if (ops->putc) |
| 285 | ops->putc += gd->reloc_off; |
| 286 | if (ops->pending) |
| 287 | ops->pending += gd->reloc_off; |
| 288 | if (ops->clear) |
| 289 | ops->clear += gd->reloc_off; |
| 290 | #if CONFIG_POST & CONFIG_SYS_POST_UART |
| 291 | if (ops->loop) |
| 292 | ops->loop += gd->reloc_off |
| 293 | #endif |
| 294 | #endif |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 295 | /* Set the baud rate */ |
| 296 | if (ops->setbrg) { |
| 297 | ret = ops->setbrg(dev, gd->baudrate); |
| 298 | if (ret) |
| 299 | return ret; |
| 300 | } |
| 301 | |
Simon Glass | 236f2bd | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 302 | #ifdef CONFIG_DM_STDIO |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 303 | if (!(gd->flags & GD_FLG_RELOC)) |
| 304 | return 0; |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 305 | memset(&sdev, '\0', sizeof(sdev)); |
| 306 | |
| 307 | strncpy(sdev.name, dev->name, sizeof(sdev.name)); |
| 308 | sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
| 309 | sdev.priv = dev; |
| 310 | sdev.putc = serial_stub_putc; |
| 311 | sdev.puts = serial_stub_puts; |
| 312 | sdev.getc = serial_stub_getc; |
| 313 | sdev.tstc = serial_stub_tstc; |
| 314 | stdio_register_dev(&sdev, &upriv->sdev); |
Simon Glass | 236f2bd | 2014-11-10 17:16:48 -0700 | [diff] [blame] | 315 | #endif |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int serial_pre_remove(struct udevice *dev) |
| 320 | { |
| 321 | #ifdef CONFIG_SYS_STDIO_DEREGISTER |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 322 | struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 323 | |
Hans de Goede | 4a74298 | 2014-10-10 18:30:17 +0200 | [diff] [blame] | 324 | if (stdio_deregister_dev(upriv->sdev, 0)) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 325 | return -EPERM; |
| 326 | #endif |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | UCLASS_DRIVER(serial) = { |
| 332 | .id = UCLASS_SERIAL, |
| 333 | .name = "serial", |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 334 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 335 | .post_probe = serial_post_probe, |
| 336 | .pre_remove = serial_pre_remove, |
| 337 | .per_device_auto_alloc_size = sizeof(struct serial_dev_priv), |
| 338 | }; |