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