]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/stdio.c
board: ti: beagle_x15: added USB initializtion code
[people/ms/u-boot.git] / common / stdio.c
CommitLineData
91d3256c 1/*
3f4978c7
HS
2 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3 *
4 * Changes for multibus/multiadapter I2C support.
5 *
91d3256c
WD
6 * (C) Copyright 2000
7 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
8 *
1a459660 9 * SPDX-License-Identifier: GPL-2.0+
91d3256c
WD
10 */
11
12#include <config.h>
13#include <common.h>
d97143a6 14#include <errno.h>
91d3256c
WD
15#include <stdarg.h>
16#include <malloc.h>
52cb4d4f 17#include <stdio_dev.h>
281e00a3 18#include <serial.h>
7f6c2cbc
WD
19#ifdef CONFIG_LOGBUFFER
20#include <logbuff.h>
21#endif
ea818dbb
HS
22
23#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
91d3256c 24#include <i2c.h>
7f6c2cbc 25#endif
91d3256c 26
d87080b7
WD
27DECLARE_GLOBAL_DATA_PTR;
28
52cb4d4f
JCPV
29static struct stdio_dev devs;
30struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
91d3256c
WD
31char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
32
6d0f6bcf
JCPV
33#if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
34#define CONFIG_SYS_DEVICE_NULLDEV 1
d791b1dc
WD
35#endif
36
32d01926
HG
37#ifdef CONFIG_SYS_STDIO_DEREGISTER
38#define CONFIG_SYS_DEVICE_NULLDEV 1
39#endif
d791b1dc 40
6d0f6bcf 41#ifdef CONFIG_SYS_DEVICE_NULLDEV
654f8d0f 42static void nulldev_putc(struct stdio_dev *dev, const char c)
91d3256c 43{
c1de7a6d 44 /* nulldev is empty! */
91d3256c
WD
45}
46
654f8d0f 47static void nulldev_puts(struct stdio_dev *dev, const char *s)
91d3256c 48{
c1de7a6d 49 /* nulldev is empty! */
91d3256c
WD
50}
51
654f8d0f 52static int nulldev_input(struct stdio_dev *dev)
91d3256c 53{
c1de7a6d
JCPV
54 /* nulldev is empty! */
55 return 0;
91d3256c
WD
56}
57#endif
58
654f8d0f 59static void stdio_serial_putc(struct stdio_dev *dev, const char c)
709ea543
SG
60{
61 serial_putc(c);
62}
63
654f8d0f 64static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
709ea543
SG
65{
66 serial_puts(s);
67}
68
654f8d0f 69static int stdio_serial_getc(struct stdio_dev *dev)
709ea543
SG
70{
71 return serial_getc();
72}
73
654f8d0f 74static int stdio_serial_tstc(struct stdio_dev *dev)
709ea543
SG
75{
76 return serial_tstc();
77}
78
91d3256c
WD
79/**************************************************************************
80 * SYSTEM DRIVERS
81 **************************************************************************
82 */
83
84static void drv_system_init (void)
85{
52cb4d4f 86 struct stdio_dev dev;
91d3256c
WD
87
88 memset (&dev, 0, sizeof (dev));
89
90 strcpy (dev.name, "serial");
91 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
709ea543
SG
92 dev.putc = stdio_serial_putc;
93 dev.puts = stdio_serial_puts;
94 dev.getc = stdio_serial_getc;
95 dev.tstc = stdio_serial_tstc;
52cb4d4f 96 stdio_register (&dev);
91d3256c 97
6d0f6bcf 98#ifdef CONFIG_SYS_DEVICE_NULLDEV
91d3256c
WD
99 memset (&dev, 0, sizeof (dev));
100
101 strcpy (dev.name, "nulldev");
102 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
103 dev.putc = nulldev_putc;
104 dev.puts = nulldev_puts;
105 dev.getc = nulldev_input;
106 dev.tstc = nulldev_input;
107
52cb4d4f 108 stdio_register (&dev);
91d3256c
WD
109#endif
110}
111
112/**************************************************************************
113 * DEVICES
114 **************************************************************************
115 */
52cb4d4f 116struct list_head* stdio_get_list(void)
c1de7a6d
JCPV
117{
118 return &(devs.list);
119}
120
d7be3056 121struct stdio_dev* stdio_get_by_name(const char *name)
c1de7a6d
JCPV
122{
123 struct list_head *pos;
52cb4d4f 124 struct stdio_dev *dev;
c1de7a6d
JCPV
125
126 if(!name)
127 return NULL;
128
129 list_for_each(pos, &(devs.list)) {
52cb4d4f 130 dev = list_entry(pos, struct stdio_dev, list);
c1de7a6d
JCPV
131 if(strcmp(dev->name, name) == 0)
132 return dev;
133 }
134
135 return NULL;
136}
137
52cb4d4f 138struct stdio_dev* stdio_clone(struct stdio_dev *dev)
628ffd73 139{
52cb4d4f 140 struct stdio_dev *_dev;
628ffd73
JCPV
141
142 if(!dev)
143 return NULL;
144
52cb4d4f 145 _dev = calloc(1, sizeof(struct stdio_dev));
628ffd73
JCPV
146
147 if(!_dev)
148 return NULL;
149
52cb4d4f 150 memcpy(_dev, dev, sizeof(struct stdio_dev));
628ffd73
JCPV
151
152 return _dev;
153}
91d3256c 154
d97143a6 155int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
91d3256c 156{
52cb4d4f 157 struct stdio_dev *_dev;
628ffd73 158
52cb4d4f 159 _dev = stdio_clone(dev);
628ffd73 160 if(!_dev)
d97143a6 161 return -ENODEV;
3e3c026e 162 list_add_tail(&(_dev->list), &(devs.list));
d97143a6
SG
163 if (devp)
164 *devp = _dev;
165
91d3256c
WD
166 return 0;
167}
168
d97143a6
SG
169int stdio_register(struct stdio_dev *dev)
170{
171 return stdio_register_dev(dev, NULL);
172}
173
91d3256c
WD
174/* deregister the device "devname".
175 * returns 0 if success, -1 if device is assigned and 1 if devname not found
176 */
52cb4d4f 177#ifdef CONFIG_SYS_STDIO_DEREGISTER
32d01926 178int stdio_deregister_dev(struct stdio_dev *dev, int force)
91d3256c 179{
c1de7a6d
JCPV
180 int l;
181 struct list_head *pos;
03bf22f5 182 char temp_names[3][16];
91d3256c 183
91d3256c
WD
184 /* get stdio devices (ListRemoveItem changes the dev list) */
185 for (l=0 ; l< MAX_FILES; l++) {
186 if (stdio_devices[l] == dev) {
32d01926
HG
187 if (force) {
188 strcpy(temp_names[l], "nulldev");
189 continue;
190 }
91d3256c
WD
191 /* Device is assigned -> report error */
192 return -1;
193 }
194 memcpy (&temp_names[l][0],
195 stdio_devices[l]->name,
03bf22f5 196 sizeof(temp_names[l]));
91d3256c 197 }
c1de7a6d
JCPV
198
199 list_del(&(dev->list));
88274b6c 200 free(dev);
c1de7a6d 201
91d3256c 202 /* reassign Device list */
c1de7a6d 203 list_for_each(pos, &(devs.list)) {
52cb4d4f 204 dev = list_entry(pos, struct stdio_dev, list);
91d3256c 205 for (l=0 ; l< MAX_FILES; l++) {
c1de7a6d 206 if(strcmp(dev->name, temp_names[l]) == 0)
91d3256c 207 stdio_devices[l] = dev;
91d3256c
WD
208 }
209 }
210 return 0;
211}
d97143a6 212
32d01926 213int stdio_deregister(const char *devname, int force)
d97143a6
SG
214{
215 struct stdio_dev *dev;
216
217 dev = stdio_get_by_name(devname);
218
219 if (!dev) /* device not found */
220 return -ENODEV;
221
32d01926 222 return stdio_deregister_dev(dev, force);
d97143a6 223}
52cb4d4f 224#endif /* CONFIG_SYS_STDIO_DEREGISTER */
91d3256c 225
9fb02491 226int stdio_init_tables(void)
91d3256c 227{
2e5167cc 228#if defined(CONFIG_NEEDS_MANUAL_RELOC)
521af04d 229 /* already relocated for current ARM implementation */
91d3256c 230 ulong relocation_offset = gd->reloc_off;
3595ac49 231 int i;
91d3256c
WD
232
233 /* relocate device name pointers */
234 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
235 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
236 relocation_offset);
237 }
2e5167cc 238#endif /* CONFIG_NEEDS_MANUAL_RELOC */
91d3256c
WD
239
240 /* Initialize the list */
c1de7a6d 241 INIT_LIST_HEAD(&(devs.list));
91d3256c 242
9fb02491
SG
243 return 0;
244}
245
246int stdio_add_devices(void)
247{
3f4978c7 248#ifdef CONFIG_SYS_I2C
3f4978c7 249 i2c_init_all();
3f4978c7 250#else
ea818dbb 251#if defined(CONFIG_HARD_I2C)
6d0f6bcf 252 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
91d3256c 253#endif
3f4978c7 254#endif
91d3256c
WD
255#ifdef CONFIG_LCD
256 drv_lcd_init ();
257#endif
a6c7ad2f 258#if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
91d3256c
WD
259 drv_video_init ();
260#endif
682011ff
WD
261#ifdef CONFIG_KEYBOARD
262 drv_keyboard_init ();
56f94be3
WD
263#endif
264#ifdef CONFIG_LOGBUFFER
265 drv_logbuff_init ();
91d3256c
WD
266#endif
267 drv_system_init ();
52cb4d4f 268 serial_stdio_init ();
232c150a
WD
269#ifdef CONFIG_USB_TTY
270 drv_usbtty_init ();
271#endif
68ceb29e
WD
272#ifdef CONFIG_NETCONSOLE
273 drv_nc_init ();
274#endif
36ea8e9a
MF
275#ifdef CONFIG_JTAG_CONSOLE
276 drv_jtag_console_init ();
277#endif
98ab435f
VB
278#ifdef CONFIG_CBMEM_CONSOLE
279 cbmemc_init();
280#endif
9fb02491
SG
281
282 return 0;
283}
284
285int stdio_init(void)
286{
287 stdio_init_tables();
288 stdio_add_devices();
289
290 return 0;
91d3256c 291}