Blame view

drivers/serial/serial.c 14.2 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
281e00a3b   wdenk   * Code cleanup
2
3
4
  /*
   * (C) Copyright 2004
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
281e00a3b   wdenk   * Code cleanup
5
6
7
   */
  
  #include <common.h>
f3998fdc4   Simon Glass   env: Rename envir...
8
  #include <env_internal.h>
db41d65a9   Simon Glass   common: Move hang...
9
  #include <hang.h>
281e00a3b   wdenk   * Code cleanup
10
  #include <serial.h>
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
11
  #include <stdio_dev.h>
7b826c2f3   Mike Frysinger   serial: implement...
12
13
  #include <post.h>
  #include <linux/compiler.h>
6d93e2580   Marek Vasut   serial: Reorder s...
14
  #include <errno.h>
281e00a3b   wdenk   * Code cleanup
15

d87080b72   Wolfgang Denk   GCC-4.x fixes: cl...
16
  DECLARE_GLOBAL_DATA_PTR;
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
17
18
  static struct serial_device *serial_devices;
  static struct serial_device *serial_current;
32057717e   Joe Hershberger   env: Add a baudra...
19
20
21
22
  /*
   * Table with supported baudrates (defined in config_xyz.h)
   */
  static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
281e00a3b   wdenk   * Code cleanup
23

9cd2b9e47   Marek Vasut   kerneldoc: Annota...
24
25
26
27
28
29
30
31
  /**
   * serial_null() - Void registration routine of a serial driver
   *
   * This routine implements a void registration routine of a serial
   * driver. The registration routine of a particular driver is aliased
   * to this empty function in case the driver is not compiled into
   * U-Boot.
   */
2a333aeb8   Marek Vasut   serial: Implement...
32
33
34
  static void serial_null(void)
  {
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
35
  /**
32057717e   Joe Hershberger   env: Add a baudra...
36
37
   * on_baudrate() - Update the actual baudrate when the env var changes
   *
938b05a5d   Heinrich Schuchardt   drivers: serial: ...
38
39
40
41
42
43
   * @name:	changed environment variable
   * @value:	new value of the environment variable
   * @op:		operation (create, overwrite, or delete)
   * @flags:	attributes of environment variable change,
   *		see flags H_* in include/search.h
   *
32057717e   Joe Hershberger   env: Add a baudra...
44
   * This will check for a valid baudrate and only apply it if valid.
938b05a5d   Heinrich Schuchardt   drivers: serial: ...
45
46
   *
   * Return:	0 on success, 1 on error
32057717e   Joe Hershberger   env: Add a baudra...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
   */
  static int on_baudrate(const char *name, const char *value, enum env_op op,
  	int flags)
  {
  	int i;
  	int baudrate;
  
  	switch (op) {
  	case env_op_create:
  	case env_op_overwrite:
  		/*
  		 * Switch to new baudrate if new baudrate is supported
  		 */
  		baudrate = simple_strtoul(value, NULL, 10);
  
  		/* Not actually changing */
  		if (gd->baudrate == baudrate)
  			return 0;
9935175f5   Axel Lin   serial: Use ARRAY...
65
  		for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
32057717e   Joe Hershberger   env: Add a baudra...
66
67
68
  			if (baudrate == baudrate_table[i])
  				break;
  		}
9935175f5   Axel Lin   serial: Use ARRAY...
69
  		if (i == ARRAY_SIZE(baudrate_table)) {
32057717e   Joe Hershberger   env: Add a baudra...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  			if ((flags & H_FORCE) == 0)
  				printf("## Baudrate %d bps not supported
  ",
  					baudrate);
  			return 1;
  		}
  		if ((flags & H_INTERACTIVE) != 0) {
  			printf("## Switch baudrate to %d"
  				" bps and press ENTER ...
  ", baudrate);
  			udelay(50000);
  		}
  
  		gd->baudrate = baudrate;
32057717e   Joe Hershberger   env: Add a baudra...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  
  		serial_setbrg();
  
  		udelay(50000);
  
  		if ((flags & H_INTERACTIVE) != 0)
  			while (1) {
  				if (getc() == '\r')
  					break;
  			}
  
  		return 0;
  	case env_op_delete:
  		printf("## Baudrate may not be deleted
  ");
  		return 1;
  	default:
  		return 0;
  	}
  }
  U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
  
  /**
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
107
108
109
110
111
112
113
114
115
   * serial_initfunc() - Forward declare of driver registration routine
   * @name:	Name of the real driver registration routine.
   *
   * This macro expands onto forward declaration of a driver registration
   * routine, which is then used below in serial_initialize() function.
   * The declaration is made weak and aliases to serial_null() so in case
   * the driver is not compiled in, the function is still declared and can
   * be used, but aliases to serial_null() and thus is optimized away.
   */
2a333aeb8   Marek Vasut   serial: Implement...
116
117
118
  #define serial_initfunc(name)					\
  	void name(void)						\
  		__attribute__((weak, alias("serial_null")));
94a255dfb   Jeroen Hofstee   serial: add proto...
119
  serial_initfunc(atmel_serial_initialize);
94a255dfb   Jeroen Hofstee   serial: add proto...
120
  serial_initfunc(mcf_serial_initialize);
94a255dfb   Jeroen Hofstee   serial: add proto...
121
  serial_initfunc(mpc85xx_serial_initialize);
a943472c2   Marek Vasut   serial: arm: Impl...
122
  serial_initfunc(mxc_serial_initialize);
34b5b76b4   Ji Luo   MA-14318-1 Suppor...
123
  serial_initfunc(xen_serial_initialize);
94a255dfb   Jeroen Hofstee   serial: add proto...
124
  serial_initfunc(ns16550_serial_initialize);
39f614779   Marek Vasut   serial: arm: Impl...
125
  serial_initfunc(pl01x_serial_initialize);
94a255dfb   Jeroen Hofstee   serial: add proto...
126
  serial_initfunc(pxa_serial_initialize);
8bdd7efa2   Marek Vasut   serial: sh: Imple...
127
  serial_initfunc(sh_serial_initialize);
44fa676e5   Weijie Gao   serial: serial_mt...
128
  serial_initfunc(mtk_serial_initialize);
0abdd9a9b   Peng Fan   MLK-18577-4 seria...
129
  serial_initfunc(xen_debug_serial_initialize);
f0eb1f61b   Marek Vasut   serial: mpc8xx: M...
130

9cd2b9e47   Marek Vasut   kerneldoc: Annota...
131
132
133
134
135
136
137
138
139
140
  /**
   * serial_register() - Register serial driver with serial driver core
   * @dev:	Pointer to the serial driver structure
   *
   * This function registers the serial driver supplied via @dev with
   * serial driver core, thus making U-Boot aware of it and making it
   * available for U-Boot to use. On platforms that still require manual
   * relocation of constant variables, relocation of the supplied structure
   * is performed.
   */
c52b4f794   Mike Frysinger   serial: drop seri...
141
  void serial_register(struct serial_device *dev)
281e00a3b   wdenk   * Code cleanup
142
  {
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
143
  #ifdef CONFIG_NEEDS_MANUAL_RELOC
f2760c4ac   Marek Vasut   serial: Enhance t...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  	if (dev->start)
  		dev->start += gd->reloc_off;
  	if (dev->stop)
  		dev->stop += gd->reloc_off;
  	if (dev->setbrg)
  		dev->setbrg += gd->reloc_off;
  	if (dev->getc)
  		dev->getc += gd->reloc_off;
  	if (dev->tstc)
  		dev->tstc += gd->reloc_off;
  	if (dev->putc)
  		dev->putc += gd->reloc_off;
  	if (dev->puts)
  		dev->puts += gd->reloc_off;
521af04d8   Peter Tyser   Conditionally per...
158
  #endif
281e00a3b   wdenk   * Code cleanup
159
160
161
  
  	dev->next = serial_devices;
  	serial_devices = dev;
281e00a3b   wdenk   * Code cleanup
162
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
163
164
165
166
167
168
169
170
171
  /**
   * serial_initialize() - Register all compiled-in serial port drivers
   *
   * This function registers all serial port drivers that are compiled
   * into the U-Boot binary with the serial core, thus making them
   * available to U-Boot to use. Lastly, this function assigns a default
   * serial port to the serial core. That serial port is then used as a
   * default output.
   */
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
172
  void serial_initialize(void)
281e00a3b   wdenk   * Code cleanup
173
  {
94a255dfb   Jeroen Hofstee   serial: add proto...
174
  	atmel_serial_initialize();
94a255dfb   Jeroen Hofstee   serial: add proto...
175
  	mcf_serial_initialize();
94a255dfb   Jeroen Hofstee   serial: add proto...
176
  	mpc85xx_serial_initialize();
a943472c2   Marek Vasut   serial: arm: Impl...
177
  	mxc_serial_initialize();
34b5b76b4   Ji Luo   MA-14318-1 Suppor...
178
  	xen_serial_initialize();
94a255dfb   Jeroen Hofstee   serial: add proto...
179
  	ns16550_serial_initialize();
39f614779   Marek Vasut   serial: arm: Impl...
180
  	pl01x_serial_initialize();
94a255dfb   Jeroen Hofstee   serial: add proto...
181
  	pxa_serial_initialize();
8bdd7efa2   Marek Vasut   serial: sh: Imple...
182
  	sh_serial_initialize();
44fa676e5   Weijie Gao   serial: serial_mt...
183
  	mtk_serial_initialize();
0abdd9a9b   Peng Fan   MLK-18577-4 seria...
184
  	xen_debug_serial_initialize();
7b953c516   Marek Vasut   serial: mips: Imp...
185

a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
186
  	serial_assign(default_serial_console()->name);
281e00a3b   wdenk   * Code cleanup
187
  }
654f8d0f3   Jeroen Hofstee   serial: make loca...
188
  static int serial_stub_start(struct stdio_dev *sdev)
709ea543b   Simon Glass   stdio: Pass devic...
189
190
191
192
193
  {
  	struct serial_device *dev = sdev->priv;
  
  	return dev->start();
  }
654f8d0f3   Jeroen Hofstee   serial: make loca...
194
  static int serial_stub_stop(struct stdio_dev *sdev)
709ea543b   Simon Glass   stdio: Pass devic...
195
196
197
198
199
  {
  	struct serial_device *dev = sdev->priv;
  
  	return dev->stop();
  }
654f8d0f3   Jeroen Hofstee   serial: make loca...
200
  static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
709ea543b   Simon Glass   stdio: Pass devic...
201
202
203
204
205
  {
  	struct serial_device *dev = sdev->priv;
  
  	dev->putc(ch);
  }
654f8d0f3   Jeroen Hofstee   serial: make loca...
206
  static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
709ea543b   Simon Glass   stdio: Pass devic...
207
208
209
210
211
  {
  	struct serial_device *dev = sdev->priv;
  
  	dev->puts(str);
  }
49ddcf3e0   Masahiro Yamada   serial: make seri...
212
  static int serial_stub_getc(struct stdio_dev *sdev)
709ea543b   Simon Glass   stdio: Pass devic...
213
214
215
216
217
  {
  	struct serial_device *dev = sdev->priv;
  
  	return dev->getc();
  }
49ddcf3e0   Masahiro Yamada   serial: make seri...
218
  static int serial_stub_tstc(struct stdio_dev *sdev)
709ea543b   Simon Glass   stdio: Pass devic...
219
220
221
222
223
  {
  	struct serial_device *dev = sdev->priv;
  
  	return dev->tstc();
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
224
225
226
227
228
229
230
  /**
   * serial_stdio_init() - Register serial ports with STDIO core
   *
   * This function generates a proxy driver for each serial port driver.
   * These proxy drivers then register with the STDIO core, making the
   * serial drivers available as STDIO devices.
   */
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
231
  void serial_stdio_init(void)
281e00a3b   wdenk   * Code cleanup
232
  {
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
233
  	struct stdio_dev dev;
281e00a3b   wdenk   * Code cleanup
234
  	struct serial_device *s = serial_devices;
2ee665339   wdenk   * Patch by Pantel...
235
  	while (s) {
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
236
  		memset(&dev, 0, sizeof(dev));
281e00a3b   wdenk   * Code cleanup
237

a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
238
  		strcpy(dev.name, s->name);
281e00a3b   wdenk   * Code cleanup
239
  		dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
709ea543b   Simon Glass   stdio: Pass devic...
240
241
242
243
244
245
  		dev.start = serial_stub_start;
  		dev.stop = serial_stub_stop;
  		dev.putc = serial_stub_putc;
  		dev.puts = serial_stub_puts;
  		dev.getc = serial_stub_getc;
  		dev.tstc = serial_stub_tstc;
addf9513d   Simon Glass   serial: Set up th...
246
  		dev.priv = s;
281e00a3b   wdenk   * Code cleanup
247

a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
248
  		stdio_register(&dev);
281e00a3b   wdenk   * Code cleanup
249
250
251
252
  
  		s = s->next;
  	}
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
253
254
255
256
257
258
259
260
261
262
263
264
  /**
   * serial_assign() - Select the serial output device by name
   * @name:	Name of the serial driver to be used as default output
   *
   * This function configures the serial output multiplexing by
   * selecting which serial device will be used as default. In case
   * the STDIO "serial" device is selected as stdin/stdout/stderr,
   * the serial device previously configured by this function will be
   * used for the particular operation.
   *
   * Returns 0 on success, negative on error.
   */
7813ca9b6   Gerlando Falauto   serial: constify ...
265
  int serial_assign(const char *name)
281e00a3b   wdenk   * Code cleanup
266
267
  {
  	struct serial_device *s;
2ee665339   wdenk   * Patch by Pantel...
268
  	for (s = serial_devices; s; s = s->next) {
6d93e2580   Marek Vasut   serial: Reorder s...
269
270
271
272
  		if (strcmp(s->name, name))
  			continue;
  		serial_current = s;
  		return 0;
281e00a3b   wdenk   * Code cleanup
273
  	}
6d93e2580   Marek Vasut   serial: Reorder s...
274
  	return -EINVAL;
281e00a3b   wdenk   * Code cleanup
275
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
276
277
278
279
280
281
  /**
   * serial_reinit_all() - Reinitialize all compiled-in serial ports
   *
   * This function reinitializes all serial ports that are compiled
   * into U-Boot by calling their serial_start() functions.
   */
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
282
  void serial_reinit_all(void)
281e00a3b   wdenk   * Code cleanup
283
284
  {
  	struct serial_device *s;
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
285
  	for (s = serial_devices; s; s = s->next)
89143fb3b   Marek Vasut   serial: Rename .i...
286
  		s->start();
281e00a3b   wdenk   * Code cleanup
287
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  /**
   * get_current() - Return pointer to currently selected serial port
   *
   * This function returns a pointer to currently selected serial port.
   * The currently selected serial port is altered by serial_assign()
   * function.
   *
   * In case this function is called before relocation or before any serial
   * port is configured, this function calls default_serial_console() to
   * determine the serial port. Otherwise, the configured serial port is
   * returned.
   *
   * Returns pointer to the currently selected serial port on success,
   * NULL on error.
   */
857c283e9   Simon Glass   Small refactor to...
303
  static struct serial_device *get_current(void)
281e00a3b   wdenk   * Code cleanup
304
  {
857c283e9   Simon Glass   Small refactor to...
305
  	struct serial_device *dev;
2ee665339   wdenk   * Patch by Pantel...
306

dee194160   Marek Vasut   serial: Reorder g...
307
  	if (!(gd->flags & GD_FLG_RELOC))
857c283e9   Simon Glass   Small refactor to...
308
  		dev = default_serial_console();
dee194160   Marek Vasut   serial: Reorder g...
309
310
311
312
  	else if (!serial_current)
  		dev = default_serial_console();
  	else
  		dev = serial_current;
857c283e9   Simon Glass   Small refactor to...
313

dee194160   Marek Vasut   serial: Reorder g...
314
315
  	/* We must have a console device */
  	if (!dev) {
f1f1e9cb7   Marek Vasut   serial: Use puts(...
316
  #ifdef CONFIG_SPL_BUILD
dee194160   Marek Vasut   serial: Reorder g...
317
318
319
  		puts("Cannot find console
  ");
  		hang();
f1f1e9cb7   Marek Vasut   serial: Use puts(...
320
  #else
dee194160   Marek Vasut   serial: Reorder g...
321
322
  		panic("Cannot find console
  ");
f1f1e9cb7   Marek Vasut   serial: Use puts(...
323
  #endif
dee194160   Marek Vasut   serial: Reorder g...
324
  	}
857c283e9   Simon Glass   Small refactor to...
325
326
  	return dev;
  }
281e00a3b   wdenk   * Code cleanup
327

9cd2b9e47   Marek Vasut   kerneldoc: Annota...
328
329
330
331
332
333
334
335
336
337
  /**
   * serial_init() - Initialize currently selected serial port
   *
   * This function initializes the currently selected serial port. This
   * usually involves setting up the registers of that particular port,
   * enabling clock and such. This function uses the get_current() call
   * to determine which port is selected.
   *
   * Returns 0 on success, negative on error.
   */
857c283e9   Simon Glass   Small refactor to...
338
339
  int serial_init(void)
  {
093f79ab8   Simon Glass   Add a flag indica...
340
  	gd->flags |= GD_FLG_SERIAL_READY;
89143fb3b   Marek Vasut   serial: Rename .i...
341
  	return get_current()->start();
281e00a3b   wdenk   * Code cleanup
342
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
343
344
345
346
347
348
349
350
351
352
  /**
   * serial_setbrg() - Configure baud-rate of currently selected serial port
   *
   * This function configures the baud-rate of the currently selected
   * serial port. The baud-rate is retrieved from global data within
   * the serial port driver. This function uses the get_current() call
   * to determine which port is selected.
   *
   * Returns 0 on success, negative on error.
   */
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
353
  void serial_setbrg(void)
281e00a3b   wdenk   * Code cleanup
354
  {
857c283e9   Simon Glass   Small refactor to...
355
  	get_current()->setbrg();
281e00a3b   wdenk   * Code cleanup
356
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
357
358
359
360
361
362
363
364
365
366
367
  /**
   * serial_getc() - Read character from currently selected serial port
   *
   * This function retrieves a character from currently selected serial
   * port. In case there is no character waiting on the serial port,
   * this function will block and wait for the character to appear. This
   * function uses the get_current() call to determine which port is
   * selected.
   *
   * Returns the character on success, negative on error.
   */
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
368
  int serial_getc(void)
281e00a3b   wdenk   * Code cleanup
369
  {
857c283e9   Simon Glass   Small refactor to...
370
  	return get_current()->getc();
281e00a3b   wdenk   * Code cleanup
371
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
372
373
374
375
376
377
378
379
380
381
  /**
   * serial_tstc() - Test if data is available on currently selected serial port
   *
   * This function tests if one or more characters are available on
   * currently selected serial port. This function never blocks. This
   * function uses the get_current() call to determine which port is
   * selected.
   *
   * Returns positive if character is available, zero otherwise.
   */
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
382
  int serial_tstc(void)
281e00a3b   wdenk   * Code cleanup
383
  {
857c283e9   Simon Glass   Small refactor to...
384
  	return get_current()->tstc();
281e00a3b   wdenk   * Code cleanup
385
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
386
387
388
389
390
391
392
393
394
395
396
  /**
   * serial_putc() - Output character via currently selected serial port
   * @c:	Single character to be output from the serial port.
   *
   * This function outputs a character via currently selected serial
   * port. This character is passed to the serial port driver responsible
   * for controlling the hardware. The hardware may still be in process
   * of transmitting another character, therefore this function may block
   * for a short amount of time. This function uses the get_current()
   * call to determine which port is selected.
   */
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
397
  void serial_putc(const char c)
281e00a3b   wdenk   * Code cleanup
398
  {
857c283e9   Simon Glass   Small refactor to...
399
  	get_current()->putc(c);
281e00a3b   wdenk   * Code cleanup
400
  }
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
401
402
403
404
405
406
407
408
409
410
411
412
413
  /**
   * serial_puts() - Output string via currently selected serial port
   * @s:	Zero-terminated string to be output from the serial port.
   *
   * This function outputs a zero-terminated string via currently
   * selected serial port. This function behaves as an accelerator
   * in case the hardware can queue multiple characters for transfer.
   * The whole string that is to be output is available to the function
   * implementing the hardware manipulation. Transmitting the whole
   * string may take some time, thus this function may block for some
   * amount of time. This function uses the get_current() call to
   * determine which port is selected.
   */
a6e6f7f4d   Gerlando Falauto   serial: cosmetic ...
414
  void serial_puts(const char *s)
281e00a3b   wdenk   * Code cleanup
415
  {
857c283e9   Simon Glass   Small refactor to...
416
  	get_current()->puts(s);
281e00a3b   wdenk   * Code cleanup
417
  }
7b826c2f3   Mike Frysinger   serial: implement...
418

9cd2b9e47   Marek Vasut   kerneldoc: Annota...
419
420
421
422
423
424
425
426
427
428
429
430
  /**
   * default_serial_puts() - Output string by calling serial_putc() in loop
   * @s:	Zero-terminated string to be output from the serial port.
   *
   * This function outputs a zero-terminated string by calling serial_putc()
   * in a loop. Most drivers do not support queueing more than one byte for
   * transfer, thus this function precisely implements their serial_puts().
   *
   * To optimize the number of get_current() calls, this function only
   * calls get_current() once and then directly accesses the putc() call
   * of the &struct serial_device .
   */
bfb7d7a3d   Marek Vasut   serial: Implement...
431
432
433
434
435
436
  void default_serial_puts(const char *s)
  {
  	struct serial_device *dev = get_current();
  	while (*s)
  		dev->putc(*s++);
  }
7b826c2f3   Mike Frysinger   serial: implement...
437
438
  #if CONFIG_POST & CONFIG_SYS_POST_UART
  static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
439
440
441
442
443
444
  /**
   * uart_post_test() - Test the currently selected serial port using POST
   * @flags:	POST framework flags
   *
   * Do a loopback test of the currently selected serial port. This
   * function is only useful in the context of the POST testing framwork.
1b25e586c   Vagrant Cascadian   Fix typo: firstly...
445
   * The serial port is first configured into loopback mode and then
9cd2b9e47   Marek Vasut   kerneldoc: Annota...
446
447
448
449
   * characters are sent through it.
   *
   * Returns 0 on success, value otherwise.
   */
7b826c2f3   Mike Frysinger   serial: implement...
450
451
452
453
454
455
456
  /* Mark weak until post/cpu/.../uart.c migrate over */
  __weak
  int uart_post_test(int flags)
  {
  	unsigned char c;
  	int ret, saved_baud, b;
  	struct serial_device *saved_dev, *s;
7b826c2f3   Mike Frysinger   serial: implement...
457
458
459
460
  
  	/* Save current serial state */
  	ret = 0;
  	saved_dev = serial_current;
8e2615752   Masahiro Yamada   bd_info: remove b...
461
  	saved_baud = gd->baudrate;
7b826c2f3   Mike Frysinger   serial: implement...
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  
  	for (s = serial_devices; s; s = s->next) {
  		/* If this driver doesn't support loop back, skip it */
  		if (!s->loop)
  			continue;
  
  		/* Test the next device */
  		serial_current = s;
  
  		ret = serial_init();
  		if (ret)
  			goto done;
  
  		/* Consume anything that happens to be queued */
  		while (serial_tstc())
  			serial_getc();
  
  		/* Enable loop back */
  		s->loop(1);
  
  		/* Test every available baud rate */
  		for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
8e2615752   Masahiro Yamada   bd_info: remove b...
484
  			gd->baudrate = bauds[b];
7b826c2f3   Mike Frysinger   serial: implement...
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  			serial_setbrg();
  
  			/*
  			 * Stick to printable chars to avoid issues:
  			 *  - terminal corruption
  			 *  - serial program reacting to sequences and sending
  			 *    back random extra data
  			 *  - most serial drivers add in extra chars (like \r
  )
  			 */
  			for (c = 0x20; c < 0x7f; ++c) {
  				/* Send it out */
  				serial_putc(c);
  
  				/* Make sure it's the same one */
  				ret = (c != serial_getc());
  				if (ret) {
  					s->loop(0);
  					goto done;
  				}
  
  				/* Clean up the output in case it was sent */
  				serial_putc('\b');
  				ret = ('\b' != serial_getc());
  				if (ret) {
  					s->loop(0);
  					goto done;
  				}
  			}
  		}
  
  		/* Disable loop back */
  		s->loop(0);
89143fb3b   Marek Vasut   serial: Rename .i...
518
519
520
  		/* XXX: There is no serial_stop() !? */
  		if (s->stop)
  			s->stop();
7b826c2f3   Mike Frysinger   serial: implement...
521
522
523
524
525
  	}
  
   done:
  	/* Restore previous serial state */
  	serial_current = saved_dev;
8e2615752   Masahiro Yamada   bd_info: remove b...
526
  	gd->baudrate = saved_baud;
7b826c2f3   Mike Frysinger   serial: implement...
527
528
529
530
531
532
  	serial_reinit_all();
  	serial_setbrg();
  
  	return ret;
  }
  #endif