blob: 9131a8f93d9a5adff359d2940ddfd6a6a05c445e [file] [log] [blame]
Simon Glass247f5962014-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 Glass593f50b2014-10-29 13:09:03 -06009#include <environment.h>
Simon Glass247f5962014-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 Glass5c60b2a2014-10-22 21:37:02 -060015#include <watchdog.h>
Simon Glass247f5962014-09-04 16:27:26 -060016#include <dm/lists.h>
17#include <dm/device-internal.h>
18
Simon Glass5c60b2a2014-10-22 21:37:02 -060019#include <ns16550.h>
20
Simon Glass247f5962014-09-04 16:27:26 -060021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass593f50b2014-10-29 13:09:03 -060023/*
24 * Table with supported baudrates (defined in config_xyz.h)
25 */
26static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
27
Simon Glass247f5962014-09-04 16:27:26 -060028#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
32static void serial_find_console_or_panic(void)
33{
Simon Glassa4c2e132014-11-10 18:00:20 -070034 struct udevice *dev;
35
Simon Glass28141d52014-09-17 09:02:40 -060036#ifdef CONFIG_OF_CONTROL
Simon Glass247f5962014-09-04 16:27:26 -060037 int node;
38
39 /* Check for a chosen console */
40 node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
41 if (node < 0)
Masahiro Yamada8d72f282014-11-21 19:47:08 +090042 node = fdt_path_offset(gd->fdt_blob, "console");
Simon Glassa4c2e132014-11-10 18:00:20 -070043 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, &dev)) {
44 gd->cur_serial_dev = dev;
Simon Glass247f5962014-09-04 16:27:26 -060045 return;
Simon Glassa4c2e132014-11-10 18:00:20 -070046 }
Simon Glass247f5962014-09-04 16:27:26 -060047
48 /*
49 * If the console is not marked to be bound before relocation, bind
50 * it anyway.
51 */
52 if (node > 0 &&
Simon Glassa4c2e132014-11-10 18:00:20 -070053 !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &dev)) {
54 if (!device_probe(dev)) {
55 gd->cur_serial_dev = dev;
Simon Glass247f5962014-09-04 16:27:26 -060056 return;
Simon Glassa4c2e132014-11-10 18:00:20 -070057 }
Simon Glass247f5962014-09-04 16:27:26 -060058 }
Simon Glass28141d52014-09-17 09:02:40 -060059#endif
Simon Glass247f5962014-09-04 16:27:26 -060060 /*
Simon Glassbc5aa992014-10-22 21:37:06 -060061 * Try to use CONFIG_CONS_INDEX if available (it is numbered from 1!).
62 *
Simon Glass247f5962014-09-04 16:27:26 -060063 * 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 */
Simon Glassbc5aa992014-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 Glassa4c2e132014-11-10 18:00:20 -070072 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))
Simon Glass247f5962014-09-04 16:27:26 -060075 panic("No serial driver found");
Simon Glassbc5aa992014-10-22 21:37:06 -060076#undef INDEX
Simon Glassa4c2e132014-11-10 18:00:20 -070077 gd->cur_serial_dev = dev;
Simon Glass247f5962014-09-04 16:27:26 -060078}
79
80/* Called prior to relocation */
81int 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 */
90void serial_initialize(void)
91{
92 serial_find_console_or_panic();
93}
94
Masahiro Yamada93da2fc2014-10-23 22:26:08 +090095static void _serial_putc(struct udevice *dev, char ch)
Simon Glass247f5962014-09-04 16:27:26 -060096{
Masahiro Yamada84df41a2014-10-23 22:26:06 +090097 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -060098 int err;
99
100 do {
Masahiro Yamada84df41a2014-10-23 22:26:06 +0900101 err = ops->putc(dev, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600102 } while (err == -EAGAIN);
103 if (ch == '\n')
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900104 _serial_putc(dev, '\r');
Simon Glass247f5962014-09-04 16:27:26 -0600105}
106
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900107static void _serial_puts(struct udevice *dev, const char *str)
Simon Glass73285a22014-10-01 19:57:23 -0600108{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900109 while (*str)
110 _serial_putc(dev, *str++);
Simon Glass73285a22014-10-01 19:57:23 -0600111}
112
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900113static int _serial_getc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600114{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900115 struct dm_serial_ops *ops = serial_get_ops(dev);
116 int err;
Simon Glass247f5962014-09-04 16:27:26 -0600117
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900118 do {
119 err = ops->getc(dev);
120 if (err == -EAGAIN)
121 WATCHDOG_RESET();
122 } while (err == -EAGAIN);
Simon Glass247f5962014-09-04 16:27:26 -0600123
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900124 return err >= 0 ? err : 0;
Simon Glass247f5962014-09-04 16:27:26 -0600125}
126
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900127static int _serial_tstc(struct udevice *dev)
Simon Glass247f5962014-09-04 16:27:26 -0600128{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900129 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glass247f5962014-09-04 16:27:26 -0600130
131 if (ops->pending)
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900132 return ops->pending(dev, true);
Simon Glass247f5962014-09-04 16:27:26 -0600133
134 return 1;
135}
136
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900137void serial_putc(char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600138{
Simon Glassa4c2e132014-11-10 18:00:20 -0700139 _serial_putc(gd->cur_serial_dev, ch);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900140}
Simon Glass247f5962014-09-04 16:27:26 -0600141
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900142void serial_puts(const char *str)
143{
Simon Glassa4c2e132014-11-10 18:00:20 -0700144 _serial_puts(gd->cur_serial_dev, str);
Simon Glass247f5962014-09-04 16:27:26 -0600145}
146
Simon Glass73285a22014-10-01 19:57:23 -0600147int serial_getc(void)
148{
Simon Glassa4c2e132014-11-10 18:00:20 -0700149 return _serial_getc(gd->cur_serial_dev);
Simon Glass73285a22014-10-01 19:57:23 -0600150}
151
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900152int serial_tstc(void)
153{
Simon Glassa4c2e132014-11-10 18:00:20 -0700154 return _serial_tstc(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900155}
156
157void serial_setbrg(void)
158{
Simon Glassa4c2e132014-11-10 18:00:20 -0700159 struct dm_serial_ops *ops = serial_get_ops(gd->cur_serial_dev);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900160
161 if (ops->setbrg)
Simon Glassa4c2e132014-11-10 18:00:20 -0700162 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900163}
164
Simon Glass247f5962014-09-04 16:27:26 -0600165void serial_stdio_init(void)
166{
167}
168
Simon Glassc058d8b2014-11-10 17:16:48 -0700169#ifdef CONFIG_DM_STDIO
Simon Glass73285a22014-10-01 19:57:23 -0600170static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
Simon Glass247f5962014-09-04 16:27:26 -0600171{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900172 _serial_putc(sdev->priv, ch);
Simon Glass247f5962014-09-04 16:27:26 -0600173}
Simon Glassc058d8b2014-11-10 17:16:48 -0700174#endif
Simon Glass247f5962014-09-04 16:27:26 -0600175
176void serial_stub_puts(struct stdio_dev *sdev, const char *str)
177{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900178 _serial_puts(sdev->priv, str);
Simon Glass247f5962014-09-04 16:27:26 -0600179}
180
181int serial_stub_getc(struct stdio_dev *sdev)
182{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900183 return _serial_getc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600184}
185
186int serial_stub_tstc(struct stdio_dev *sdev)
187{
Masahiro Yamada93da2fc2014-10-23 22:26:08 +0900188 return _serial_tstc(sdev->priv);
Simon Glass247f5962014-09-04 16:27:26 -0600189}
190
Simon Glass593f50b2014-10-29 13:09:03 -0600191/**
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 */
196static 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}
250U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
Simon Glass247f5962014-09-04 16:27:26 -0600251
252static int serial_post_probe(struct udevice *dev)
253{
Simon Glass247f5962014-09-04 16:27:26 -0600254 struct dm_serial_ops *ops = serial_get_ops(dev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700255#ifdef CONFIG_DM_STDIO
Simon Glass247f5962014-09-04 16:27:26 -0600256 struct serial_dev_priv *upriv = dev->uclass_priv;
Simon Glassc058d8b2014-11-10 17:16:48 -0700257 struct stdio_dev sdev;
258#endif
Simon Glass247f5962014-09-04 16:27:26 -0600259 int ret;
260
261 /* Set the baud rate */
262 if (ops->setbrg) {
263 ret = ops->setbrg(dev, gd->baudrate);
264 if (ret)
265 return ret;
266 }
267
Simon Glassc058d8b2014-11-10 17:16:48 -0700268#ifdef CONFIG_DM_STDIO
Simon Glass247f5962014-09-04 16:27:26 -0600269 if (!(gd->flags & GD_FLG_RELOC))
270 return 0;
Simon Glass247f5962014-09-04 16:27:26 -0600271 memset(&sdev, '\0', sizeof(sdev));
272
273 strncpy(sdev.name, dev->name, sizeof(sdev.name));
274 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
275 sdev.priv = dev;
276 sdev.putc = serial_stub_putc;
277 sdev.puts = serial_stub_puts;
278 sdev.getc = serial_stub_getc;
279 sdev.tstc = serial_stub_tstc;
280 stdio_register_dev(&sdev, &upriv->sdev);
Simon Glassc058d8b2014-11-10 17:16:48 -0700281#endif
Simon Glass247f5962014-09-04 16:27:26 -0600282 return 0;
283}
284
285static int serial_pre_remove(struct udevice *dev)
286{
287#ifdef CONFIG_SYS_STDIO_DEREGISTER
288 struct serial_dev_priv *upriv = dev->uclass_priv;
289
Hans de Goede4c4150c2014-10-10 18:30:17 +0200290 if (stdio_deregister_dev(upriv->sdev, 0))
Simon Glass247f5962014-09-04 16:27:26 -0600291 return -EPERM;
292#endif
293
294 return 0;
295}
296
297UCLASS_DRIVER(serial) = {
298 .id = UCLASS_SERIAL,
299 .name = "serial",
Simon Glass0ccb0972015-01-25 08:27:05 -0700300 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass247f5962014-09-04 16:27:26 -0600301 .post_probe = serial_post_probe,
302 .pre_remove = serial_pre_remove,
303 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
304};