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