blob: 4f50911f668af3927b796a42e9b978f327abb293 [file] [log] [blame]
wdenk281e00a2004-08-01 22:48:16 +00001/*
2 * (C) Copyright 2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <serial.h>
26#include <devices.h>
27
28#if defined(CONFIG_SERIAL_MULTI)
29
30static struct serial_device *serial_devices = NULL;
31static struct serial_device *serial_current = NULL;
32
33#ifndef CONFIG_LWMON
wdenk2ee66532004-10-11 22:43:02 +000034struct serial_device *default_serial_console (void)
wdenk281e00a2004-08-01 22:48:16 +000035{
36#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
37 return &serial_smc_device;
38#elif defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
39 || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
40 return &serial_scc_device;
41#else
42#error No default console
43#endif
44}
45#endif
46
wdenk2ee66532004-10-11 22:43:02 +000047static int serial_register (struct serial_device *dev)
wdenk281e00a2004-08-01 22:48:16 +000048{
49 DECLARE_GLOBAL_DATA_PTR;
50
51 dev->init += gd->reloc_off;
52 dev->setbrg += gd->reloc_off;
53 dev->getc += gd->reloc_off;
54 dev->tstc += gd->reloc_off;
55 dev->putc += gd->reloc_off;
56 dev->puts += gd->reloc_off;
57
58 dev->next = serial_devices;
59 serial_devices = dev;
60
61 return 0;
62}
63
wdenk2ee66532004-10-11 22:43:02 +000064void serial_initialize (void)
wdenk281e00a2004-08-01 22:48:16 +000065{
66#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
wdenk2ee66532004-10-11 22:43:02 +000067 serial_register (&serial_smc_device);
wdenk281e00a2004-08-01 22:48:16 +000068#endif
69#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
70 || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
wdenk2ee66532004-10-11 22:43:02 +000071 serial_register (&serial_scc_device);
wdenk281e00a2004-08-01 22:48:16 +000072#endif
73
wdenk2ee66532004-10-11 22:43:02 +000074 serial_assign (default_serial_console ()->name);
wdenk281e00a2004-08-01 22:48:16 +000075}
76
wdenk2ee66532004-10-11 22:43:02 +000077void serial_devices_init (void)
wdenk281e00a2004-08-01 22:48:16 +000078{
79 device_t dev;
80 struct serial_device *s = serial_devices;
81
wdenk2ee66532004-10-11 22:43:02 +000082 while (s) {
wdenk281e00a2004-08-01 22:48:16 +000083 memset (&dev, 0, sizeof (dev));
84
85 strcpy (dev.name, s->name);
86 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
87
88 dev.start = s->init;
89 dev.putc = s->putc;
90 dev.puts = s->puts;
91 dev.getc = s->getc;
92 dev.tstc = s->tstc;
93
94 device_register (&dev);
95
96 s = s->next;
97 }
98}
99
wdenk2ee66532004-10-11 22:43:02 +0000100int serial_assign (char *name)
wdenk281e00a2004-08-01 22:48:16 +0000101{
102 struct serial_device *s;
103
wdenk2ee66532004-10-11 22:43:02 +0000104 for (s = serial_devices; s; s = s->next) {
105 if (strcmp (s->name, name) == 0) {
wdenk281e00a2004-08-01 22:48:16 +0000106 serial_current = s;
107 return 0;
108 }
109 }
110
111 return 1;
112}
113
wdenk2ee66532004-10-11 22:43:02 +0000114void serial_reinit_all (void)
wdenk281e00a2004-08-01 22:48:16 +0000115{
116 struct serial_device *s;
117
wdenk2ee66532004-10-11 22:43:02 +0000118 for (s = serial_devices; s; s = s->next) {
119 s->init ();
wdenk281e00a2004-08-01 22:48:16 +0000120 }
121}
122
wdenk2ee66532004-10-11 22:43:02 +0000123int serial_init (void)
wdenk281e00a2004-08-01 22:48:16 +0000124{
wdenk2ee66532004-10-11 22:43:02 +0000125 DECLARE_GLOBAL_DATA_PTR;
wdenk8b74bf32004-10-11 23:10:30 +0000126
wdenk2ee66532004-10-11 22:43:02 +0000127 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
128 struct serial_device *dev = default_serial_console ();
129
130 return dev->init ();
wdenk281e00a2004-08-01 22:48:16 +0000131 }
132
wdenk2ee66532004-10-11 22:43:02 +0000133 return serial_current->init ();
wdenk281e00a2004-08-01 22:48:16 +0000134}
135
wdenk2ee66532004-10-11 22:43:02 +0000136void serial_setbrg (void)
wdenk281e00a2004-08-01 22:48:16 +0000137{
wdenk2ee66532004-10-11 22:43:02 +0000138 DECLARE_GLOBAL_DATA_PTR;
wdenk8b74bf32004-10-11 23:10:30 +0000139
wdenk2ee66532004-10-11 22:43:02 +0000140 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
141 struct serial_device *dev = default_serial_console ();
142
143 dev->setbrg ();
wdenk281e00a2004-08-01 22:48:16 +0000144 return;
145 }
146
wdenk2ee66532004-10-11 22:43:02 +0000147 serial_current->setbrg ();
wdenk281e00a2004-08-01 22:48:16 +0000148}
149
wdenk2ee66532004-10-11 22:43:02 +0000150int serial_getc (void)
wdenk281e00a2004-08-01 22:48:16 +0000151{
wdenk2ee66532004-10-11 22:43:02 +0000152 DECLARE_GLOBAL_DATA_PTR;
wdenk8b74bf32004-10-11 23:10:30 +0000153
wdenk2ee66532004-10-11 22:43:02 +0000154 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
155 struct serial_device *dev = default_serial_console ();
156
157 return dev->getc ();
wdenk281e00a2004-08-01 22:48:16 +0000158 }
159
wdenk2ee66532004-10-11 22:43:02 +0000160 return serial_current->getc ();
wdenk281e00a2004-08-01 22:48:16 +0000161}
162
wdenk2ee66532004-10-11 22:43:02 +0000163int serial_tstc (void)
wdenk281e00a2004-08-01 22:48:16 +0000164{
wdenk2ee66532004-10-11 22:43:02 +0000165 DECLARE_GLOBAL_DATA_PTR;
wdenk8b74bf32004-10-11 23:10:30 +0000166
wdenk2ee66532004-10-11 22:43:02 +0000167 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
168 struct serial_device *dev = default_serial_console ();
169
170 return dev->tstc ();
wdenk281e00a2004-08-01 22:48:16 +0000171 }
172
wdenk2ee66532004-10-11 22:43:02 +0000173 return serial_current->tstc ();
wdenk281e00a2004-08-01 22:48:16 +0000174}
175
wdenk2ee66532004-10-11 22:43:02 +0000176void serial_putc (const char c)
wdenk281e00a2004-08-01 22:48:16 +0000177{
wdenk2ee66532004-10-11 22:43:02 +0000178 DECLARE_GLOBAL_DATA_PTR;
wdenk8b74bf32004-10-11 23:10:30 +0000179
wdenk2ee66532004-10-11 22:43:02 +0000180 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
181 struct serial_device *dev = default_serial_console ();
182
183 dev->putc (c);
wdenk281e00a2004-08-01 22:48:16 +0000184 return;
185 }
186
wdenk2ee66532004-10-11 22:43:02 +0000187 serial_current->putc (c);
wdenk281e00a2004-08-01 22:48:16 +0000188}
189
wdenk2ee66532004-10-11 22:43:02 +0000190void serial_puts (const char *s)
wdenk281e00a2004-08-01 22:48:16 +0000191{
wdenk2ee66532004-10-11 22:43:02 +0000192 DECLARE_GLOBAL_DATA_PTR;
wdenk8b74bf32004-10-11 23:10:30 +0000193
wdenk2ee66532004-10-11 22:43:02 +0000194 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
195 struct serial_device *dev = default_serial_console ();
196
197 dev->puts (s);
wdenk281e00a2004-08-01 22:48:16 +0000198 return;
199 }
200
wdenk2ee66532004-10-11 22:43:02 +0000201 serial_current->puts (s);
wdenk281e00a2004-08-01 22:48:16 +0000202}
203
204#endif /* CONFIG_SERIAL_MULTI */