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> |
| 9 | #include <errno.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <os.h> |
| 12 | #include <serial.h> |
| 13 | #include <stdio_dev.h> |
Simon Glass | c487fd4 | 2014-10-22 21:37:02 -0600 | [diff] [blame] | 14 | #include <watchdog.h> |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 15 | #include <dm/lists.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | |
Simon Glass | c487fd4 | 2014-10-22 21:37:02 -0600 | [diff] [blame] | 18 | #include <ns16550.h> |
| 19 | |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | /* The currently-selected console serial device */ |
| 23 | struct udevice *cur_dev __attribute__ ((section(".data"))); |
| 24 | |
| 25 | #ifndef CONFIG_SYS_MALLOC_F_LEN |
| 26 | #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work" |
| 27 | #endif |
| 28 | |
| 29 | static void serial_find_console_or_panic(void) |
| 30 | { |
Simon Glass | 59990bf | 2014-09-17 09:02:40 -0600 | [diff] [blame] | 31 | #ifdef CONFIG_OF_CONTROL |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 32 | int node; |
| 33 | |
| 34 | /* Check for a chosen console */ |
| 35 | node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path"); |
| 36 | if (node < 0) |
| 37 | node = fdtdec_get_alias_node(gd->fdt_blob, "console"); |
| 38 | if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev)) |
| 39 | return; |
| 40 | |
| 41 | /* |
| 42 | * If the console is not marked to be bound before relocation, bind |
| 43 | * it anyway. |
| 44 | */ |
| 45 | if (node > 0 && |
| 46 | !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) { |
| 47 | if (!device_probe(cur_dev)) |
| 48 | return; |
| 49 | cur_dev = NULL; |
| 50 | } |
Simon Glass | 59990bf | 2014-09-17 09:02:40 -0600 | [diff] [blame] | 51 | #endif |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 52 | /* |
Simon Glass | 91155c6 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 53 | * Try to use CONFIG_CONS_INDEX if available (it is numbered from 1!). |
| 54 | * |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 55 | * Failing that, get the device with sequence number 0, or in extremis |
| 56 | * just the first serial device we can find. But we insist on having |
| 57 | * a console (even if it is silent). |
| 58 | */ |
Simon Glass | 91155c6 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 59 | #ifdef CONFIG_CONS_INDEX |
| 60 | #define INDEX (CONFIG_CONS_INDEX - 1) |
| 61 | #else |
| 62 | #define INDEX 0 |
| 63 | #endif |
| 64 | if (uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &cur_dev) && |
| 65 | uclass_get_device(UCLASS_SERIAL, INDEX, &cur_dev) && |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 66 | (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev)) |
| 67 | panic("No serial driver found"); |
Simon Glass | 91155c6 | 2014-10-22 21:37:06 -0600 | [diff] [blame] | 68 | #undef INDEX |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* Called prior to relocation */ |
| 72 | int serial_init(void) |
| 73 | { |
| 74 | serial_find_console_or_panic(); |
| 75 | gd->flags |= GD_FLG_SERIAL_READY; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* Called after relocation */ |
| 81 | void serial_initialize(void) |
| 82 | { |
| 83 | serial_find_console_or_panic(); |
| 84 | } |
| 85 | |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 86 | static void serial_putc_dev(struct udevice *dev, char ch) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 87 | { |
Masahiro Yamada | bac6446 | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 88 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 89 | int err; |
| 90 | |
| 91 | do { |
Masahiro Yamada | bac6446 | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 92 | err = ops->putc(dev, ch); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 93 | } while (err == -EAGAIN); |
| 94 | if (ch == '\n') |
Masahiro Yamada | bac6446 | 2014-10-23 22:26:06 +0900 | [diff] [blame] | 95 | serial_putc_dev(dev, '\r'); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 98 | void serial_putc(char ch) |
| 99 | { |
| 100 | serial_putc_dev(cur_dev, ch); |
| 101 | } |
| 102 | |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 103 | void serial_setbrg(void) |
| 104 | { |
| 105 | struct dm_serial_ops *ops = serial_get_ops(cur_dev); |
| 106 | |
| 107 | if (ops->setbrg) |
| 108 | ops->setbrg(cur_dev, gd->baudrate); |
| 109 | } |
| 110 | |
| 111 | void serial_puts(const char *str) |
| 112 | { |
| 113 | while (*str) |
| 114 | serial_putc(*str++); |
| 115 | } |
| 116 | |
| 117 | int serial_tstc(void) |
| 118 | { |
| 119 | struct dm_serial_ops *ops = serial_get_ops(cur_dev); |
| 120 | |
| 121 | if (ops->pending) |
| 122 | return ops->pending(cur_dev, true); |
| 123 | |
| 124 | return 1; |
| 125 | } |
| 126 | |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 127 | static int serial_getc_dev(struct udevice *dev) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 128 | { |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 129 | struct dm_serial_ops *ops = serial_get_ops(dev); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 130 | int err; |
| 131 | |
| 132 | do { |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 133 | err = ops->getc(dev); |
Simon Glass | c487fd4 | 2014-10-22 21:37:02 -0600 | [diff] [blame] | 134 | if (err == -EAGAIN) |
| 135 | WATCHDOG_RESET(); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 136 | } while (err == -EAGAIN); |
| 137 | |
| 138 | return err >= 0 ? err : 0; |
| 139 | } |
| 140 | |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 141 | int serial_getc(void) |
| 142 | { |
| 143 | return serial_getc_dev(cur_dev); |
| 144 | } |
| 145 | |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 146 | void serial_stdio_init(void) |
| 147 | { |
| 148 | } |
| 149 | |
Simon Glass | b889332 | 2014-10-01 19:57:23 -0600 | [diff] [blame] | 150 | static void serial_stub_putc(struct stdio_dev *sdev, const char ch) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 151 | { |
Masahiro Yamada | 52a0a6f | 2014-10-23 22:26:07 +0900 | [diff] [blame^] | 152 | serial_putc_dev(sdev->priv, ch); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void serial_stub_puts(struct stdio_dev *sdev, const char *str) |
| 156 | { |
| 157 | while (*str) |
| 158 | serial_stub_putc(sdev, *str++); |
| 159 | } |
| 160 | |
| 161 | int serial_stub_getc(struct stdio_dev *sdev) |
| 162 | { |
Masahiro Yamada | 52a0a6f | 2014-10-23 22:26:07 +0900 | [diff] [blame^] | 163 | return serial_getc_dev(sdev->priv); |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | int serial_stub_tstc(struct stdio_dev *sdev) |
| 167 | { |
| 168 | struct udevice *dev = sdev->priv; |
| 169 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 170 | |
| 171 | if (ops->pending) |
| 172 | return ops->pending(dev, true); |
| 173 | |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | static int serial_post_probe(struct udevice *dev) |
| 178 | { |
| 179 | struct stdio_dev sdev; |
| 180 | struct dm_serial_ops *ops = serial_get_ops(dev); |
| 181 | struct serial_dev_priv *upriv = dev->uclass_priv; |
| 182 | int ret; |
| 183 | |
| 184 | /* Set the baud rate */ |
| 185 | if (ops->setbrg) { |
| 186 | ret = ops->setbrg(dev, gd->baudrate); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | if (!(gd->flags & GD_FLG_RELOC)) |
| 192 | return 0; |
| 193 | |
| 194 | memset(&sdev, '\0', sizeof(sdev)); |
| 195 | |
| 196 | strncpy(sdev.name, dev->name, sizeof(sdev.name)); |
| 197 | sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
| 198 | sdev.priv = dev; |
| 199 | sdev.putc = serial_stub_putc; |
| 200 | sdev.puts = serial_stub_puts; |
| 201 | sdev.getc = serial_stub_getc; |
| 202 | sdev.tstc = serial_stub_tstc; |
| 203 | stdio_register_dev(&sdev, &upriv->sdev); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int serial_pre_remove(struct udevice *dev) |
| 209 | { |
| 210 | #ifdef CONFIG_SYS_STDIO_DEREGISTER |
| 211 | struct serial_dev_priv *upriv = dev->uclass_priv; |
| 212 | |
Hans de Goede | 4a74298 | 2014-10-10 18:30:17 +0200 | [diff] [blame] | 213 | if (stdio_deregister_dev(upriv->sdev, 0)) |
Simon Glass | 57d9275 | 2014-09-04 16:27:26 -0600 | [diff] [blame] | 214 | return -EPERM; |
| 215 | #endif |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | UCLASS_DRIVER(serial) = { |
| 221 | .id = UCLASS_SERIAL, |
| 222 | .name = "serial", |
| 223 | .post_probe = serial_post_probe, |
| 224 | .pre_remove = serial_pre_remove, |
| 225 | .per_device_auto_alloc_size = sizeof(struct serial_dev_priv), |
| 226 | }; |