blob: 815fec32646924c2c8000cbb25798cd6b227460a [file] [log] [blame]
Simon Glass57d92752014-09-04 16:27:26 -06001/*
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>
Simon Glassad1b81c2014-10-29 13:09:03 -06009#include <environment.h>
Simon Glass57d92752014-09-04 16:27:26 -060010#include <errno.h>
11#include <fdtdec.h>
12#include <os.h>
13#include <serial.h>
14#include <stdio_dev.h>
Simon Glassc487fd42014-10-22 21:37:02 -060015#include <watchdog.h>
Simon Glass57d92752014-09-04 16:27:26 -060016#include <dm/lists.h>
17#include <dm/device-internal.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassad1b81c2014-10-29 13:09:03 -060021/*
22 * Table with supported baudrates (defined in config_xyz.h)
23 */
24static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
25
Simon Glass57d92752014-09-04 16:27:26 -060026#ifndef CONFIG_SYS_MALLOC_F_LEN
27#error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
28#endif
29
30static void serial_find_console_or_panic(void)
31{
Simon Glass469a5792014-11-10 18:00:20 -070032 struct udevice *dev;
Simon Glass57d92752014-09-04 16:27:26 -060033 int node;
34
Simon Glassaf282242015-03-06 13:19:03 -070035 if (OF_CONTROL && gd->fdt_blob) {
Simon Glasse9fc0582015-05-12 14:55:05 -060036 /* Check for a chosen console */
37 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
38 if (node < 0)
39 node = fdt_path_offset(gd->fdt_blob, "console");
40 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
41 &dev)) {
Simon Glass469a5792014-11-10 18:00:20 -070042 gd->cur_serial_dev = dev;
Simon Glass57d92752014-09-04 16:27:26 -060043 return;
Simon Glass469a5792014-11-10 18:00:20 -070044 }
Simon Glasse9fc0582015-05-12 14:55:05 -060045
46 /*
47 * If the console is not marked to be bound before relocation,
48 * bind it anyway.
49 */
50 if (node > 0 &&
51 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &dev)) {
52 if (!device_probe(dev)) {
53 gd->cur_serial_dev = dev;
54 return;
55 }
56 }
Simon Glassaf282242015-03-06 13:19:03 -070057 }
58 if (!SPL_BUILD || !OF_CONTROL || !gd->fdt_blob) {
Simon Glasse9fc0582015-05-12 14:55:05 -060059 /*
60 * Try to use CONFIG_CONS_INDEX if available (it is numbered
61 * from 1!).
62 *
63 * Failing that, get the device with sequence number 0, or in
64 * extremis just the first serial device we can find. But we
65 * insist on having a console (even if it is silent).
66 */
Simon Glass91155c62014-10-22 21:37:06 -060067#ifdef CONFIG_CONS_INDEX
68#define INDEX (CONFIG_CONS_INDEX - 1)
69#else
70#define INDEX 0
71#endif
Simon Glasse9fc0582015-05-12 14:55:05 -060072 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 gd->cur_serial_dev = dev;
76 return;
77 }
Simon Glass91155c62014-10-22 21:37:06 -060078#undef INDEX
Simon Glasse9fc0582015-05-12 14:55:05 -060079 }
80
81 panic_str("No serial driver found");
Simon Glass57d92752014-09-04 16:27:26 -060082}
83
84/* Called prior to relocation */
85int serial_init(void)
86{
87 serial_find_console_or_panic();
88 gd->flags |= GD_FLG_SERIAL_READY;
89
90 return 0;
91}
92
93/* Called after relocation */
94void serial_initialize(void)
95{
96 serial_find_console_or_panic();
97}
98
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +090099static void _serial_putc(struct udevice *dev, char ch)
Simon Glass57d92752014-09-04 16:27:26 -0600100{
Masahiro Yamadabac64462014-10-23 22:26:06 +0900101 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600102 int err;
103
104 do {
Masahiro Yamadabac64462014-10-23 22:26:06 +0900105 err = ops->putc(dev, ch);
Simon Glass57d92752014-09-04 16:27:26 -0600106 } while (err == -EAGAIN);
107 if (ch == '\n')
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900108 _serial_putc(dev, '\r');
Simon Glass57d92752014-09-04 16:27:26 -0600109}
110
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900111static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass57d92752014-09-04 16:27:26 -0600112{
113 while (*str)
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900114 _serial_putc(dev, *str++);
Simon Glass57d92752014-09-04 16:27:26 -0600115}
116
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900117static int _serial_getc(struct udevice *dev)
Simon Glass57d92752014-09-04 16:27:26 -0600118{
Simon Glassb8893322014-10-01 19:57:23 -0600119 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600120 int err;
121
122 do {
Simon Glassb8893322014-10-01 19:57:23 -0600123 err = ops->getc(dev);
Simon Glassc487fd42014-10-22 21:37:02 -0600124 if (err == -EAGAIN)
125 WATCHDOG_RESET();
Simon Glass57d92752014-09-04 16:27:26 -0600126 } while (err == -EAGAIN);
127
128 return err >= 0 ? err : 0;
129}
130
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900131static int _serial_tstc(struct udevice *dev)
132{
133 struct dm_serial_ops *ops = serial_get_ops(dev);
134
135 if (ops->pending)
136 return ops->pending(dev, true);
137
138 return 1;
139}
140
141void serial_putc(char ch)
142{
Simon Glass469a5792014-11-10 18:00:20 -0700143 _serial_putc(gd->cur_serial_dev, ch);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900144}
145
146void serial_puts(const char *str)
147{
Simon Glass469a5792014-11-10 18:00:20 -0700148 _serial_puts(gd->cur_serial_dev, str);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900149}
150
Simon Glassb8893322014-10-01 19:57:23 -0600151int serial_getc(void)
152{
Simon Glass469a5792014-11-10 18:00:20 -0700153 return _serial_getc(gd->cur_serial_dev);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900154}
155
156int serial_tstc(void)
157{
Simon Glass469a5792014-11-10 18:00:20 -0700158 return _serial_tstc(gd->cur_serial_dev);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900159}
160
161void serial_setbrg(void)
162{
Simon Glass469a5792014-11-10 18:00:20 -0700163 struct dm_serial_ops *ops = serial_get_ops(gd->cur_serial_dev);
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900164
165 if (ops->setbrg)
Simon Glass469a5792014-11-10 18:00:20 -0700166 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
Simon Glassb8893322014-10-01 19:57:23 -0600167}
168
Simon Glass57d92752014-09-04 16:27:26 -0600169void serial_stdio_init(void)
170{
171}
172
Simon Glass236f2bd2014-11-10 17:16:48 -0700173#ifdef CONFIG_DM_STDIO
Simon Glassb8893322014-10-01 19:57:23 -0600174static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass57d92752014-09-04 16:27:26 -0600175{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900176 _serial_putc(sdev->priv, ch);
Simon Glass57d92752014-09-04 16:27:26 -0600177}
Simon Glass236f2bd2014-11-10 17:16:48 -0700178#endif
Simon Glass57d92752014-09-04 16:27:26 -0600179
180void serial_stub_puts(struct stdio_dev *sdev, const char *str)
181{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900182 _serial_puts(sdev->priv, str);
Simon Glass57d92752014-09-04 16:27:26 -0600183}
184
185int serial_stub_getc(struct stdio_dev *sdev)
186{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900187 return _serial_getc(sdev->priv);
Simon Glass57d92752014-09-04 16:27:26 -0600188}
189
190int serial_stub_tstc(struct stdio_dev *sdev)
191{
Masahiro Yamadaeea5a4c2014-10-23 22:26:08 +0900192 return _serial_tstc(sdev->priv);
Simon Glass57d92752014-09-04 16:27:26 -0600193}
194
Simon Glassad1b81c2014-10-29 13:09:03 -0600195/**
196 * on_baudrate() - Update the actual baudrate when the env var changes
197 *
198 * This will check for a valid baudrate and only apply it if valid.
199 */
200static int on_baudrate(const char *name, const char *value, enum env_op op,
201 int flags)
202{
203 int i;
204 int baudrate;
205
206 switch (op) {
207 case env_op_create:
208 case env_op_overwrite:
209 /*
210 * Switch to new baudrate if new baudrate is supported
211 */
212 baudrate = simple_strtoul(value, NULL, 10);
213
214 /* Not actually changing */
215 if (gd->baudrate == baudrate)
216 return 0;
217
218 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
219 if (baudrate == baudrate_table[i])
220 break;
221 }
222 if (i == ARRAY_SIZE(baudrate_table)) {
223 if ((flags & H_FORCE) == 0)
224 printf("## Baudrate %d bps not supported\n",
225 baudrate);
226 return 1;
227 }
228 if ((flags & H_INTERACTIVE) != 0) {
229 printf("## Switch baudrate to %d bps and press ENTER ...\n",
230 baudrate);
231 udelay(50000);
232 }
233
234 gd->baudrate = baudrate;
235
236 serial_setbrg();
237
238 udelay(50000);
239
240 if ((flags & H_INTERACTIVE) != 0)
241 while (1) {
242 if (getc() == '\r')
243 break;
244 }
245
246 return 0;
247 case env_op_delete:
248 printf("## Baudrate may not be deleted\n");
249 return 1;
250 default:
251 return 0;
252 }
253}
254U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
255
Simon Glass57d92752014-09-04 16:27:26 -0600256static int serial_post_probe(struct udevice *dev)
257{
Simon Glass57d92752014-09-04 16:27:26 -0600258 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass236f2bd2014-11-10 17:16:48 -0700259#ifdef CONFIG_DM_STDIO
Simon Glasse564f052015-03-05 12:25:20 -0700260 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass236f2bd2014-11-10 17:16:48 -0700261 struct stdio_dev sdev;
262#endif
Simon Glass57d92752014-09-04 16:27:26 -0600263 int ret;
264
Michal Simek484fdf52015-02-02 16:31:59 +0100265#if defined(CONFIG_NEEDS_MANUAL_RELOC)
266 if (ops->setbrg)
267 ops->setbrg += gd->reloc_off;
268 if (ops->getc)
269 ops->getc += gd->reloc_off;
270 if (ops->putc)
271 ops->putc += gd->reloc_off;
272 if (ops->pending)
273 ops->pending += gd->reloc_off;
274 if (ops->clear)
275 ops->clear += gd->reloc_off;
276#if CONFIG_POST & CONFIG_SYS_POST_UART
277 if (ops->loop)
278 ops->loop += gd->reloc_off
279#endif
280#endif
Simon Glass57d92752014-09-04 16:27:26 -0600281 /* Set the baud rate */
282 if (ops->setbrg) {
283 ret = ops->setbrg(dev, gd->baudrate);
284 if (ret)
285 return ret;
286 }
287
Simon Glass236f2bd2014-11-10 17:16:48 -0700288#ifdef CONFIG_DM_STDIO
Simon Glass57d92752014-09-04 16:27:26 -0600289 if (!(gd->flags & GD_FLG_RELOC))
290 return 0;
Simon Glass57d92752014-09-04 16:27:26 -0600291 memset(&sdev, '\0', sizeof(sdev));
292
293 strncpy(sdev.name, dev->name, sizeof(sdev.name));
294 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
295 sdev.priv = dev;
296 sdev.putc = serial_stub_putc;
297 sdev.puts = serial_stub_puts;
298 sdev.getc = serial_stub_getc;
299 sdev.tstc = serial_stub_tstc;
300 stdio_register_dev(&sdev, &upriv->sdev);
Simon Glass236f2bd2014-11-10 17:16:48 -0700301#endif
Simon Glass57d92752014-09-04 16:27:26 -0600302 return 0;
303}
304
305static int serial_pre_remove(struct udevice *dev)
306{
307#ifdef CONFIG_SYS_STDIO_DEREGISTER
Simon Glasse564f052015-03-05 12:25:20 -0700308 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass57d92752014-09-04 16:27:26 -0600309
Hans de Goede4a742982014-10-10 18:30:17 +0200310 if (stdio_deregister_dev(upriv->sdev, 0))
Simon Glass57d92752014-09-04 16:27:26 -0600311 return -EPERM;
312#endif
313
314 return 0;
315}
316
317UCLASS_DRIVER(serial) = {
318 .id = UCLASS_SERIAL,
319 .name = "serial",
Simon Glass9cc36a22015-01-25 08:27:05 -0700320 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass57d92752014-09-04 16:27:26 -0600321 .post_probe = serial_post_probe,
322 .pre_remove = serial_pre_remove,
323 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
324};