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 <environment.h> 10 #include <errno.h> 11 #include <fdtdec.h> 12 #include <os.h> 13 #include <serial.h> 14 #include <stdio_dev.h> 15 #include <watchdog.h> 16 #include <dm/lists.h> 17 #include <dm/device-internal.h> 18 19 #include <ns16550.h> 20 21 DECLARE_GLOBAL_DATA_PTR; 22 23 /* The currently-selected console serial device */ 24 struct udevice *cur_dev __attribute__ ((section(".data"))); 25 26 /* 27 * Table with supported baudrates (defined in config_xyz.h) 28 */ 29 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; 30 31 #ifndef CONFIG_SYS_MALLOC_F_LEN 32 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work" 33 #endif 34 35 static void serial_find_console_or_panic(void) 36 { 37 #ifdef CONFIG_OF_CONTROL 38 int node; 39 40 /* Check for a chosen console */ 41 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path"); 42 if (node < 0) 43 node = fdtdec_get_alias_node(gd->fdt_blob, "console"); 44 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &cur_dev)) 45 return; 46 47 /* 48 * If the console is not marked to be bound before relocation, bind 49 * it anyway. 50 */ 51 if (node > 0 && 52 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &cur_dev)) { 53 if (!device_probe(cur_dev)) 54 return; 55 cur_dev = NULL; 56 } 57 #endif 58 /* 59 * Try to use CONFIG_CONS_INDEX if available (it is numbered from 1!). 60 * 61 * Failing that, get the device with sequence number 0, or in extremis 62 * just the first serial device we can find. But we insist on having 63 * a console (even if it is silent). 64 */ 65 #ifdef CONFIG_CONS_INDEX 66 #define INDEX (CONFIG_CONS_INDEX - 1) 67 #else 68 #define INDEX 0 69 #endif 70 if (uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &cur_dev) && 71 uclass_get_device(UCLASS_SERIAL, INDEX, &cur_dev) && 72 (uclass_first_device(UCLASS_SERIAL, &cur_dev) || !cur_dev)) 73 panic("No serial driver found"); 74 #undef INDEX 75 } 76 77 /* Called prior to relocation */ 78 int serial_init(void) 79 { 80 serial_find_console_or_panic(); 81 gd->flags |= GD_FLG_SERIAL_READY; 82 83 return 0; 84 } 85 86 /* Called after relocation */ 87 void serial_initialize(void) 88 { 89 serial_find_console_or_panic(); 90 } 91 92 static void _serial_putc(struct udevice *dev, char ch) 93 { 94 struct dm_serial_ops *ops = serial_get_ops(dev); 95 int err; 96 97 do { 98 err = ops->putc(dev, ch); 99 } while (err == -EAGAIN); 100 if (ch == '\n') 101 _serial_putc(dev, '\r'); 102 } 103 104 static void _serial_puts(struct udevice *dev, const char *str) 105 { 106 while (*str) 107 _serial_putc(dev, *str++); 108 } 109 110 static int _serial_getc(struct udevice *dev) 111 { 112 struct dm_serial_ops *ops = serial_get_ops(dev); 113 int err; 114 115 do { 116 err = ops->getc(dev); 117 if (err == -EAGAIN) 118 WATCHDOG_RESET(); 119 } while (err == -EAGAIN); 120 121 return err >= 0 ? err : 0; 122 } 123 124 static int _serial_tstc(struct udevice *dev) 125 { 126 struct dm_serial_ops *ops = serial_get_ops(dev); 127 128 if (ops->pending) 129 return ops->pending(dev, true); 130 131 return 1; 132 } 133 134 void serial_putc(char ch) 135 { 136 _serial_putc(cur_dev, ch); 137 } 138 139 void serial_puts(const char *str) 140 { 141 _serial_puts(cur_dev, str); 142 } 143 144 int serial_getc(void) 145 { 146 return _serial_getc(cur_dev); 147 } 148 149 int serial_tstc(void) 150 { 151 return _serial_tstc(cur_dev); 152 } 153 154 void serial_setbrg(void) 155 { 156 struct dm_serial_ops *ops = serial_get_ops(cur_dev); 157 158 if (ops->setbrg) 159 ops->setbrg(cur_dev, gd->baudrate); 160 } 161 162 void serial_stdio_init(void) 163 { 164 } 165 166 static void serial_stub_putc(struct stdio_dev *sdev, const char ch) 167 { 168 _serial_putc(sdev->priv, ch); 169 } 170 171 void serial_stub_puts(struct stdio_dev *sdev, const char *str) 172 { 173 _serial_puts(sdev->priv, str); 174 } 175 176 int serial_stub_getc(struct stdio_dev *sdev) 177 { 178 return _serial_getc(sdev->priv); 179 } 180 181 int serial_stub_tstc(struct stdio_dev *sdev) 182 { 183 return _serial_tstc(sdev->priv); 184 } 185 186 /** 187 * on_baudrate() - Update the actual baudrate when the env var changes 188 * 189 * This will check for a valid baudrate and only apply it if valid. 190 */ 191 static int on_baudrate(const char *name, const char *value, enum env_op op, 192 int flags) 193 { 194 int i; 195 int baudrate; 196 197 switch (op) { 198 case env_op_create: 199 case env_op_overwrite: 200 /* 201 * Switch to new baudrate if new baudrate is supported 202 */ 203 baudrate = simple_strtoul(value, NULL, 10); 204 205 /* Not actually changing */ 206 if (gd->baudrate == baudrate) 207 return 0; 208 209 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { 210 if (baudrate == baudrate_table[i]) 211 break; 212 } 213 if (i == ARRAY_SIZE(baudrate_table)) { 214 if ((flags & H_FORCE) == 0) 215 printf("## Baudrate %d bps not supported\n", 216 baudrate); 217 return 1; 218 } 219 if ((flags & H_INTERACTIVE) != 0) { 220 printf("## Switch baudrate to %d bps and press ENTER ...\n", 221 baudrate); 222 udelay(50000); 223 } 224 225 gd->baudrate = baudrate; 226 227 serial_setbrg(); 228 229 udelay(50000); 230 231 if ((flags & H_INTERACTIVE) != 0) 232 while (1) { 233 if (getc() == '\r') 234 break; 235 } 236 237 return 0; 238 case env_op_delete: 239 printf("## Baudrate may not be deleted\n"); 240 return 1; 241 default: 242 return 0; 243 } 244 } 245 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); 246 247 static int serial_post_probe(struct udevice *dev) 248 { 249 struct stdio_dev sdev; 250 struct dm_serial_ops *ops = serial_get_ops(dev); 251 struct serial_dev_priv *upriv = dev->uclass_priv; 252 int ret; 253 254 /* Set the baud rate */ 255 if (ops->setbrg) { 256 ret = ops->setbrg(dev, gd->baudrate); 257 if (ret) 258 return ret; 259 } 260 261 if (!(gd->flags & GD_FLG_RELOC)) 262 return 0; 263 264 memset(&sdev, '\0', sizeof(sdev)); 265 266 strncpy(sdev.name, dev->name, sizeof(sdev.name)); 267 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; 268 sdev.priv = dev; 269 sdev.putc = serial_stub_putc; 270 sdev.puts = serial_stub_puts; 271 sdev.getc = serial_stub_getc; 272 sdev.tstc = serial_stub_tstc; 273 stdio_register_dev(&sdev, &upriv->sdev); 274 275 return 0; 276 } 277 278 static int serial_pre_remove(struct udevice *dev) 279 { 280 #ifdef CONFIG_SYS_STDIO_DEREGISTER 281 struct serial_dev_priv *upriv = dev->uclass_priv; 282 283 if (stdio_deregister_dev(upriv->sdev, 0)) 284 return -EPERM; 285 #endif 286 287 return 0; 288 } 289 290 UCLASS_DRIVER(serial) = { 291 .id = UCLASS_SERIAL, 292 .name = "serial", 293 .post_probe = serial_post_probe, 294 .pre_remove = serial_pre_remove, 295 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv), 296 }; 297