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