blob: 4ea8c0cedf8285c1011dd9e35b371c0958ff03be [file] [log] [blame]
wdenk91d32562002-09-18 21:21:13 +00001/*
2 * (C) Copyright 2000
3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk91d32562002-09-18 21:21:13 +00006 */
7
8#include <config.h>
9#include <common.h>
10#include <stdarg.h>
11#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020012#include <stdio_dev.h>
wdenk7ac16102004-08-01 22:48:16 +000013#include <serial.h>
wdenk1272e232002-11-10 22:06:23 +000014#ifdef CONFIG_LOGBUFFER
15#include <logbuff.h>
16#endif
17#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
wdenk91d32562002-09-18 21:21:13 +000018#include <i2c.h>
wdenk1272e232002-11-10 22:06:23 +000019#endif
wdenk91d32562002-09-18 21:21:13 +000020
Wolfgang Denk6405a152006-03-31 18:32:53 +020021DECLARE_GLOBAL_DATA_PTR;
22
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020023static struct stdio_dev devs;
24struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
wdenk91d32562002-09-18 21:21:13 +000025char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
26
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020027#if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
28#define CONFIG_SYS_DEVICE_NULLDEV 1
wdenk92bbe3f2003-04-20 14:04:18 +000029#endif
30
31
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020032#ifdef CONFIG_SYS_DEVICE_NULLDEV
wdenk91d32562002-09-18 21:21:13 +000033void nulldev_putc(const char c)
34{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020035 /* nulldev is empty! */
wdenk91d32562002-09-18 21:21:13 +000036}
37
38void nulldev_puts(const char *s)
39{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020040 /* nulldev is empty! */
wdenk91d32562002-09-18 21:21:13 +000041}
42
43int nulldev_input(void)
44{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020045 /* nulldev is empty! */
46 return 0;
wdenk91d32562002-09-18 21:21:13 +000047}
48#endif
49
50/**************************************************************************
51 * SYSTEM DRIVERS
52 **************************************************************************
53 */
54
55static void drv_system_init (void)
56{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020057 struct stdio_dev dev;
wdenk91d32562002-09-18 21:21:13 +000058
59 memset (&dev, 0, sizeof (dev));
60
61 strcpy (dev.name, "serial");
62 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
wdenk91d32562002-09-18 21:21:13 +000063 dev.putc = serial_putc;
64 dev.puts = serial_puts;
65 dev.getc = serial_getc;
66 dev.tstc = serial_tstc;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020067 stdio_register (&dev);
wdenk91d32562002-09-18 21:21:13 +000068
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020069#ifdef CONFIG_SYS_DEVICE_NULLDEV
wdenk91d32562002-09-18 21:21:13 +000070 memset (&dev, 0, sizeof (dev));
71
72 strcpy (dev.name, "nulldev");
73 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
74 dev.putc = nulldev_putc;
75 dev.puts = nulldev_puts;
76 dev.getc = nulldev_input;
77 dev.tstc = nulldev_input;
78
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020079 stdio_register (&dev);
wdenk91d32562002-09-18 21:21:13 +000080#endif
81}
82
83/**************************************************************************
84 * DEVICES
85 **************************************************************************
86 */
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020087struct list_head* stdio_get_list(void)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020088{
89 return &(devs.list);
90}
91
Mike Frysingere3d55722010-10-20 07:18:03 -040092struct stdio_dev* stdio_get_by_name(const char *name)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020093{
94 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020095 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +020096
97 if(!name)
98 return NULL;
99
100 list_for_each(pos, &(devs.list)) {
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200101 dev = list_entry(pos, struct stdio_dev, list);
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200102 if(strcmp(dev->name, name) == 0)
103 return dev;
104 }
105
106 return NULL;
107}
108
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200109struct stdio_dev* stdio_clone(struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200110{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200111 struct stdio_dev *_dev;
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200112
113 if(!dev)
114 return NULL;
115
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200116 _dev = calloc(1, sizeof(struct stdio_dev));
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200117
118 if(!_dev)
119 return NULL;
120
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200121 memcpy(_dev, dev, sizeof(struct stdio_dev));
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200122
123 return _dev;
124}
wdenk91d32562002-09-18 21:21:13 +0000125
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200126int stdio_register (struct stdio_dev * dev)
wdenk91d32562002-09-18 21:21:13 +0000127{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200128 struct stdio_dev *_dev;
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200129
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200130 _dev = stdio_clone(dev);
Jean-Christophe PLAGNIOL-VILLARD3d85c432008-09-01 17:11:26 +0200131 if(!_dev)
132 return -1;
Stefan Roesec3be13b2008-09-05 10:47:46 +0200133 list_add_tail(&(_dev->list), &(devs.list));
wdenk91d32562002-09-18 21:21:13 +0000134 return 0;
135}
136
137/* deregister the device "devname".
138 * returns 0 if success, -1 if device is assigned and 1 if devname not found
139 */
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200140#ifdef CONFIG_SYS_STDIO_DEREGISTER
Mike Frysingere3d55722010-10-20 07:18:03 -0400141int stdio_deregister(const char *devname)
wdenk91d32562002-09-18 21:21:13 +0000142{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200143 int l;
144 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200145 struct stdio_dev *dev;
Bradley Bolen466d7392011-08-22 11:48:05 +0000146 char temp_names[3][16];
wdenk91d32562002-09-18 21:21:13 +0000147
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200148 dev = stdio_get_by_name(devname);
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200149
150 if(!dev) /* device not found */
151 return -1;
wdenk91d32562002-09-18 21:21:13 +0000152 /* get stdio devices (ListRemoveItem changes the dev list) */
153 for (l=0 ; l< MAX_FILES; l++) {
154 if (stdio_devices[l] == dev) {
155 /* Device is assigned -> report error */
156 return -1;
157 }
158 memcpy (&temp_names[l][0],
159 stdio_devices[l]->name,
Bradley Bolen466d7392011-08-22 11:48:05 +0000160 sizeof(temp_names[l]));
wdenk91d32562002-09-18 21:21:13 +0000161 }
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200162
163 list_del(&(dev->list));
164
wdenk91d32562002-09-18 21:21:13 +0000165 /* reassign Device list */
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200166 list_for_each(pos, &(devs.list)) {
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200167 dev = list_entry(pos, struct stdio_dev, list);
wdenk91d32562002-09-18 21:21:13 +0000168 for (l=0 ; l< MAX_FILES; l++) {
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200169 if(strcmp(dev->name, temp_names[l]) == 0)
wdenk91d32562002-09-18 21:21:13 +0000170 stdio_devices[l] = dev;
wdenk91d32562002-09-18 21:21:13 +0000171 }
172 }
173 return 0;
174}
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200175#endif /* CONFIG_SYS_STDIO_DEREGISTER */
wdenk91d32562002-09-18 21:21:13 +0000176
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200177int stdio_init (void)
wdenk91d32562002-09-18 21:21:13 +0000178{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200179#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500180 /* already relocated for current ARM implementation */
wdenk91d32562002-09-18 21:21:13 +0000181 ulong relocation_offset = gd->reloc_off;
wdenk56958612003-06-22 17:18:28 +0000182 int i;
wdenk91d32562002-09-18 21:21:13 +0000183
184 /* relocate device name pointers */
185 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
186 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
187 relocation_offset);
188 }
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200189#endif /* CONFIG_NEEDS_MANUAL_RELOC */
wdenk91d32562002-09-18 21:21:13 +0000190
191 /* Initialize the list */
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200192 INIT_LIST_HEAD(&(devs.list));
wdenk91d32562002-09-18 21:21:13 +0000193
Michal Simek0828cf22013-01-22 23:40:06 +0000194#ifdef CONFIG_ARM_DCC
Jean-Christophe PLAGNIOL-VILLARDb8023102009-02-22 15:49:28 +0100195 drv_arm_dcc_init ();
196#endif
wdenk91d32562002-09-18 21:21:13 +0000197#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200198 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenk91d32562002-09-18 21:21:13 +0000199#endif
200#ifdef CONFIG_LCD
201 drv_lcd_init ();
202#endif
wdenk9dd2b882002-12-03 21:28:10 +0000203#if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
wdenk91d32562002-09-18 21:21:13 +0000204 drv_video_init ();
205#endif
wdenk4e112c12003-06-03 23:54:09 +0000206#ifdef CONFIG_KEYBOARD
207 drv_keyboard_init ();
wdenk91d32562002-09-18 21:21:13 +0000208#endif
wdenk56f94be2002-11-05 16:35:14 +0000209#ifdef CONFIG_LOGBUFFER
210 drv_logbuff_init ();
211#endif
wdenk91d32562002-09-18 21:21:13 +0000212 drv_system_init ();
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200213 serial_stdio_init ();
wdenk29e7f5a2004-03-12 00:14:09 +0000214#ifdef CONFIG_USB_TTY
215 drv_usbtty_init ();
216#endif
wdenkb8fb6192004-08-02 21:11:11 +0000217#ifdef CONFIG_NETCONSOLE
218 drv_nc_init ();
219#endif
Mike Frysinger7504e972008-10-11 21:51:20 -0400220#ifdef CONFIG_JTAG_CONSOLE
221 drv_jtag_console_init ();
222#endif
Vadim Bendebury263a8bd2012-10-12 18:48:47 +0000223#ifdef CONFIG_CBMEM_CONSOLE
224 cbmemc_init();
225#endif
wdenk91d32562002-09-18 21:21:13 +0000226 return (0);
227}